棉花糖模式中未返回相关字段(也包括flask / sqlalchemy)

时间:2019-02-09 12:59:14

标签: python sqlalchemy flask-sqlalchemy marshmallow

使用marshmallow 2.18.0,flask-marshmallow 0.9.0。

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

问题是AnswerSchema根本没有显示next_question的超链接或任何数据,我正尝试使用flask-marshmallow的HyperlinkRelated来获取这些链接。功能。但是,它对question是有效的(em {em}是有效的问题)。

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

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

顺便说一下,当我查询Nested时,Question在另一个方向上效果很好:

$ 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)

1 个答案:

答案 0 :(得分:1)

感谢lftl on Reddit提供了答案。我只需要向Question添加backref。

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,并且猴子补丁很好用。