我正在尝试使用 SQLAlchemy、Marshmallow 和 FLask 从我的 Submission_status、Policy 类中查询表。我正在尝试创建一个路由调用 summary.py 以使用 GET 请求为我的前端获取类似这样的内容:
"policy_id": "X0002",
"policy_holder": "Mr Bean",
"policy_holder_contact":"0123456789"
"policy_holder_email":"bean@hotmail.com"
"product_id":"1",
"status": "incomplete",
"description":"Pending UW",
"days_in_current_status":"3",
"sales_name":"Ali",
"sales_contact":"0123454666",
"sales_email":"ali@message.com",
"submission_date":"12/12/2022
我想从下表中获取这些数据:
'policy_id': Submission_status.policy_id,
'policy_holder':Policy.policy_holder,
'policy_holder_contact':Policy.policy_holder_contact,
'policy_holder_email':Policy.policy_holder_email,
'product_id':Policy.product_id,
'status':Submission_status.status,
'description':Submission_status.description,
'num_days':(func.age(datetime.utcnow().strftime('%B %d %Y'),Submission_status.status_date)/86400).label('days'),
'sales_name':Policy.sales_name,
'sales_contact':Policy.sales_contact,
'sales_email':Policy.sales_email,
'submission_date':Submission_status.status_date
我的模型和模式如下所示:
''models.py''
#Policy Model
class Policy(db.Model):
id = db.Column(db.Integer, primary_key=True)
sub_date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
product_id=db.Column(db.Integer, db.ForeignKey('product.id'), nullable=False)
policy_holder=db.Column(db.String(50),nullable=False)
policy_holder_contact=db.Column(db.String(50))
policy_holder_email=db.Column(db.String(50))
sales_name=db.Column(db.String(50))
sales_contact=db.Column(db.String(50))
sales_email=db.Column(db.String(50))
sales_date=db.Column(db.DateTime)
def __init__ (self,sub_date,product_id,policy_holder,policy_holder_contact,policy_holder_email,sales_name,sales_contact,sales_email,sales_date):
self.sub_date=sub_date
self.product_id=product_id
self.policy_holder=policy_holder
self.policy_holder_contact=policy_holder_contact
self.policy_holder_email=policy_holder_email
self.sales_name=sales_name
self.sales_contact=sales_contact
self.sales_email=sales_email
self.sales_date=sales_date
#Policy schema
class PolicySchema(ma.Schema):
class Meta:
fields=('id','sub_date','product_id','policy_holder','policy_holder_contact','policy_holder_email','sales_name','sales_contact','sales_email','sales_date')
policy_schema=PolicySchema()
policys_schema=PolicySchema(many=True)
#Submission_status Model
class Submission_status(db.Model):
id=db.Column(db.Integer,primary_key=True)
status_date=db.Column(db.DateTime,nullable=False,default=datetime.utcnow)
policy_id=db.Column(db.Integer,db.ForeignKey('policy.id'),nullable=False)
policy = db.relationship('Policy', backref='submission_status')
description=db.Column(db.String(200))
status=db.Column(db.String(50),nullable=False)
user_id=db.Column(db.Integer,db.ForeignKey('user.id'),nullable=False)
def __init__ (self,status_date,policy_id,description,status,user_id):
self.status_date=status_date
self.policy_id=policy_id
self.description=description
self.status=status
self.user_id=user_id
class SubmissionStatusSchema(ma.Schema):
class Meta:
fields=('id','status_date','policy_id','description','status','user_id','user.name','days')
submissionstatus_schema=SubmissionStatusSchema()
submissionstatuss_schema=SubmissionStatusSchema(many=True)
#Summary Schema
class SummarySchema(ma.Schema):
class Meta:
fields = ('submission_status.policy_id'
'policy.policy_holder',
'policy.policy_holder_contact',
'policy.policy_holder_email',
'policy.product_id',
'submission_status.status',
'submission_status.description',
'policy.sales_name',
'policy.sales_contact',
'policy.sales_email',
'submission_status.status_date,',
)
summary_schema = SummarySchema()
summarys_schema = SummarySchema(many=True)
不要介意错误的缩进,它在项目中缩进并且适用于其他功能。
这是我在summary.py中的代码:
@summary.route("/", methods = ['GET'])
def get_summary():
summary = db.session.query(Submission_status, Policy).outerjoin(Submission_status,Policy.id == Submission_status.policy_id).all()
result = submissionstatuss_schema.dump(summary)
return jsonify(result)
谁可以帮我这个事?当我运行服务器和在网络上时,我总是得到空括号......