【发布时间】:2019-11-04 04:33:12
【问题描述】:
我目前正在制作一个带有 get 请求的 API,以返回 2 个模型的连接 json 数据,这些模型使用 flask、sqlalchemy 和 flask-sqlalchemy(用于查询)和 flask-marshmallow(用于序列化为 JSON)具有关系
我的模型:
class Recording(Base):
__tablename__ = 'recordings'
id = Column(Integer, primary_key=True)
filepath = Column(String, nullable=False)
language_id = Column(Integer, ForeignKey('languages.id'), nullable=False)
language = relationship("Language", back_populates="recordings")
user_id = Column(Integer, ForeignKey('users.id'), nullable=False)
user = relationship("User", back_populates="recordings")
created_at = Column(DateTime, nullable=False, default=func.now())
updated_at = Column(DateTime, nullable=False,
default=func.now(), onupdate=func.now())
deletedd_at = Column(DateTime, nullable=True)
def __repr__(self):
return 'id: {}'.format(self.id)
User.recordings = relationship("Recording", order_by=Recording.id, back_populates="user")
Language.recordings = relationship("Recording", order_by=Recording.id, back_populates="language")
class RecordingResult(Base):
__tablename__ = 'recording_results'
id = Column(Integer, primary_key=True)
is_with_dictionary = Column(Boolean, default=False)
result = Column(String, nullable=True)
run_time = Column(Float, default=0.0)
recording_id = Column(Integer, ForeignKey('recordings.id'), nullable=False)
recording = relationship("Recording", back_populates="recording_results", lazy="joined")
speech_service_id = Column(Integer, ForeignKey('speech_services.id'), nullable=False)
speech_service = relationship("SpeechService", back_populates="recording_results")
created_at = Column(DateTime, nullable=False, default=func.now())
updated_at = Column(DateTime, nullable=False,
default=func.now(), onupdate=func.now())
deletedd_at = Column(DateTime, nullable=True)
def __repr__(self):
return 'id: {}'.format(self.id)
Recording.recording_results = relationship("RecordingResult", order_by=RecordingResult.id, back_populates="recording", lazy="joined")
SpeechService.recording_results = relationship("RecordingResult", order_by=RecordingResult.id, back_populates="speech_service")
我的架构:
class RecordingResultItemSchema(ma.Schema):
class Meta:
model = RecordingResult
fields = ('id', 'is_with_dictionary', 'result', 'run_time', 'recording_id', 'speech_service_id')
class RecordingItemSchema(ma.Schema):
class Meta:
model = Recording
fields = ('id', 'filepath', 'language_id', 'user_id', 'recording_results')
recording_results = ma.Nested(RecordingResultItemSchema)
recording_item_schema = RecordingItemSchema()
recording_items_schema = RecordingItemSchema(many=True)
recording_result_item_schema = RecordingResultItemSchema()
recording_result_items_schema = RecordingResultItemSchema(many=True)
Recording模型有很多RecordingResult(外键为recording_id)
我在 API 中使用了这个查询,使用 sqlalchemy 的“lazy”来获取 2 个模型的连接数据:
@app.route('/recording', methods=['GET'])
def get_recording_item():
if db.session.query(Recording).options(joinedload(Recording.recording_results)).all():
recording_list = db.session.query(Recording).options(joinedload(Recording.recording_results)).all()
result = recording_items_schema.dump(recording_list)
return jsonify(result.data), 200
else:
return jsonify(msg="Object not found"), 400
通过邮递员发送请求后,我收到了错误消息:
AttributeError:“recording_id”不是有效字段 对于 [id: 1]。 // Werkzeug 调试器
在控制台中我得到了这个错误:
'"{0}" 不是 {1} 的有效字段。'.format(key, obj)) app_1
| AttributeError: "recording_id" 不是 [id: 1] 的有效字段。
我打印了查询并得到了结果,但是当我尝试使用模式时,这些字段似乎无效。我按照 flask-marshmallow 的文档使用嵌套对象,我做错了什么?任何帮助将不胜感激
【问题讨论】:
标签: python flask nested schema marshmallow