【问题标题】:Flask Marshmallow relationship only dumps id'sFlask Marshmallow 关系只转储 id
【发布时间】:2019-12-19 11:05:42
【问题描述】:

我遇到了烧瓶棉花糖和 SQAlchemy 的问题,我不知道如何解决。

我有两个模型:

class User(db.Model):

   __tablename__ = "user"

   id = db.Column(db.Integer, primary_key=True)
   rol = db.Column(db.String(15), nullable=False, default="user")
   tasks = db.relationship("Task", backref="user", lazy=True)

   def __init__(self, username: str, password: str, rol: str = "user"):

      self.username = username
      self.password = password
      self.rol = rol

class Task(db.Model):

   __tablename__ = "task"

   id = db.Column(db.Integer, primary_key=True)
   title = db.Column(db.String(50), nullable=True)
   description = db.Column(db.String(255), nullable=False)
   timestamp = db.Column(db.DateTime)
   user_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)

   def __init__(self, title: str, description: str):

      self.title = title
      self.description = description
      self.timestamp = datetime.now()

然后我定义了两个模式:

class UserSchema(ma.ModelSchema):

    class Meta:
        model = User
        load_only = ("password",)

class TaskSchema(ma.ModelSchema):

    class Meta:
        model = Task
        include_fk = True
        dump_only = ("timestamp",)

当执行用户模型的转储时,我只会得到一个 taks_id 列表作为任务,如下所示:

{
    "user": {
        "id": 3,
        "tasks": [
            2,
            3,
            4,
            5,
            6,
            7
        ],
        "username": "dhouard1",
        "rol": "admin"
    }
}

但是,当我在 UserSchema 中添加嵌套模式时,例如:

tasks = ma.Nested("TaskSchema")

我有一个空集:

{
    "user": {
        "id": 3,
        "rol": "admin",
        "username": "dhouard1",
        "tasks": {}
    }
}

我有什么遗漏的吗?

【问题讨论】:

    标签: python marshmallow


    【解决方案1】:

    最后,我必须解决这个问题。我在嵌套模式中添加了 many 参数,就像这样:

    tasks = ma.Nested("TaskSchema", many=True)
    

    现在我有了完整的结果。

    {
        "user": {
            "rol": "admin",
            "username": "dhouard1",
            "id": 3,
            "tasks": [
                {
                    "description": "descripcion de tarea insertada 2",
                    "title": "descripcion de tarea insertada 2",
                    "timestamp": "2019-12-13T13:28:09",
                    "id": 2
                },
                {
                    "description": "descripcion de tarea insertada 2",
                    "title": "descripcion de tarea insertada 2",
                    "timestamp": "2019-12-13T13:31:32",
                    "id": 3
                },
                {
                    "description": "descripcion de tarea insertada 2",
                    "title": "descripcion de tarea insertada 2",
                    "timestamp": "2019-12-13T13:36:27",
                    "id": 4
                },
                {
                    "description": "descripcion de tarea insertada 2",
                    "title": "descripcion de tarea insertada 2",
                    "timestamp": "2019-12-13T14:04:34",
                    "id": 5
                },
                {
                    "description": "descripcion de tarea insertada 3",
                    "title": "descripcion de tarea insertada 3",
                    "timestamp": "2019-12-13T14:38:59",
                    "id": 6
                },
                {
                    "description": "descripcion de tarea insertada 3",
                    "title": "descripcion de tarea insertada 3",
                    "timestamp": "2019-12-16T11:47:33",
                    "id": 7
                }
            ]
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-14
      • 2020-07-30
      • 1970-01-01
      • 1970-01-01
      • 2016-10-14
      • 2020-02-11
      • 2019-10-01
      • 2021-08-09
      相关资源
      最近更新 更多