1 回答

TA貢獻1856條經驗 獲得超11個贊
除非您打算提供自定義 SQL 函數,否則您將希望避免使用.expression 修飾符并繼續使用實例級方法。
? ? @hybrid_property
? ? def create_presigned_url(self):
? ? ? ? """Generate a presigned URL to share an S3 object
? ? ? ? ? ? return: Presigned URL as string. If error, returns None.
? ? ? ? """
? ? ? ??
? ? ? ? bucket_name = 'myBucket'
? ? ? ? object_name = 'pdfs/{}'.format(self.file_name)
? ? ? ? expiration = 3600
? ? ? ? # Generate a presigned URL for the S3 object
? ? ? ? try:
? ? ? ? ? ? response = bucket_resource.generate_presigned_url(
? ? ? ? ? ? ? ? "get_object",
? ? ? ? ? ? ? ? Params={"Bucket": bucket_name, "Key": object_name},
? ? ? ? ? ? ? ? ExpiresIn=expiration,
? ? ? ? ? ? )
? ? ? ? except Exception as e:
? ? ? ? ? ? return None
? ??
? ? ? ? # The response contains the presigned URL
? ? ? ? return response
要使用它,您需要在實例級別而不是類級別進行操作。所以你的查詢是針對實例的:
instance = db.session.query(EnrollmentFormData).filter(EnrollmentFormData.id==1).first()
print(instance.create_presigned_url)
#or
instance = db.session.query(EnrollmentFormData).get(1)
print(instance.create_presigned_url)
#or
print([x.create_presigned_url for x in db.session.query(EnrollmentFormData).all()]
添加回答
舉報