marshmallow 模式中未返回相关字段(还有 flask / sqlalchemy)

Related field not returned in marshmallow schema (also flask / sqlalchemy)

使用 marshmallow 2.18.0,flask-marshmallow 0.9.0。

我正在构建一个处理一系列问题和答案的系统。显示的下一个问题取决于上一个问题的答案。为了对此建模,我有两个 Answer 指向 Question.

的外键

问题是 AnswerSchema 不显示超链接 的任何数据 next_question,我正试图将其引入使用 flask-marshmallow 的 HyperlinkRelated 函数。但是 question 有效(答案是 ing 的问题)。

$ http localhost:5000/answers/1/
{
    "answer": "Great",
    "id": 1,
    "question": "/questions/1/", *# Works, but where is next_question?*
}

如果相关,我确实在 answer_schema._declared_fields 中看到了 next_question,但在 answer_schema.dump(answer).data 中看到了 而不是

顺便说一句,当我查询 Question:

时,Nested 在另一个方向上效果很好
$ http localhost:5000/questions/1/
{
    "answers": [
        {
            "answer": "Great",
            "id": 1,
            "question": "/questions/1/",
        },
        {
            "answer": "More than great",
            "id": 2,
            "question": "/questions/1/",
        }
    ],
    "id": 1,
    "question": "How are you doing today?",
}

无论如何,我不确定 HyperlinkRelated 是否是解决此问题的正确方法,但如果不是,我不知道该怎么做。我真的很感激理解什么是正确的方法(我也应该在另一个方向使用 Nested 吗?!?!),以及 为什么 即我所拥有的在文档中遗漏了。

这里是相关的(为了简短起见,我删除了所有可能的内容,它是来自不同文件的混合):

from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
from sqlalchemy_mixins import AllFeaturesMixin

db = SQLAlchemy()
ma = Marshmallow()

class GenericMixin:
    id = db.Column(db.Integer, primary_key=True)

class BaseModel(db.Model, AllFeaturesMixin, GenericMixin):
    __abstract__ = True

BaseModel.set_session(db.session)

from flask_classful import FlaskView
from webargs import fields
from webargs.flaskparser import use_kwargs

from .resources.user import User, User

class Question(BaseModel):
    question = db.Column(db.String(128), unique=True, nullable=False)
    answers = db.relationship('Answer', backref='question',
                              foreign_keys='Answer.question_id')

class QuestionSchema(ma.ModelSchema):
    class Meta:
        model = Question

    answers = ma.Nested(AnswerSchema, many=True, strict=True)

question_schema = QuestionSchema(strict=True)

class QuestionsView(FlaskView):
    def get(self, id):
        question = Question.query.get_or_404(id)
        return question_schema.jsonify(question)

class Answer(BaseModel):
    answer = db.Column(db.String(128), unique=True, nullable=False)
    question_id = db.Column(db.Integer,
                            db.ForeignKey('question.id'),
                            nullable=False)
    next_question_id = db.Column(db.Integer,
                                 db.ForeignKey('question.id'),
                                 nullable=True)

class AnswerSchema(ma.ModelSchema):
    class Meta:
        model = Answer

    question = ma.HyperlinkRelated('QuestionsView:get')
    # HELP! How do I get this to return the link to the next question?
    next_question = ma.HyperlinkRelated('QuestionsView:get')

answer_schema = AnswerSchema(strict=True)

class AnswersView(FlaskView): 
    def get(self, id):
        answer = Answer.query.get_or_404(id)
        return answer_schema.jsonify(answer)

感谢 lftl on Reddit 提供答案。我只需要将 backref 添加到 Question.

class Question(BaseModel):
    question = db.Column(db.String(128), unique=True, nullable=False)
    answers = db.relationship(
        "Answer", backref="question", foreign_keys="Answer.question_id"
    )
    next_question = db.relationship(
        "Answer", backref="next_question", foreign_keys="Answer.next_question_id"
    )

Reddit link 包含其他有用的讨论。我发现 HyperlinkRelated 不支持外键空值,但是有一个开放的 PR,猴子补丁很好用。