0

我正在尝试获得嵌套的序列化输出,但我得到的只是多对一表主键的“多”端。

预期输出:

[{'part_numbers': 
                 {'part_number': '23103048', 'description': 'blue product'},
  'machines': 2,
  'percent_complete': 5.0,
  'serial_number': '001',
  'timestamp_created': 2020-03-12T15:4:23.135098},
 {'part_numbers': 
                 {'part_number': '44444009', 'description': 'red product'},
  'machines': 1,
  'percent_complete': 60.0,
  'serial_number': '002',
  'timestamp_created': '2020-03-12T15:44:23.135098'}]

实际输出:

[{'id': 3,
  'machines': 2,
  'percent_complete': 5.0,
  'serial_number': '001',
  'timestamp_created': 2020-03-12T15:4:23.135098},
 {'id': 1,
  'machines': 1,
  'percent_complete': 60.0,
  'serial_number': '0002',
  'timestamp_created': '2020-03-12T15:44:23.135098'}]

这些是我的文件。我已经尝试添加part_numbers = ma.Nested(PartNumbers)Machine里面的架构models.py,但它并没有改变结果!

my_app
|--- my_app
     |--- \__init\__.py
     |--- models.py
     |--- views.py

模型.py

from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
from marshmallow_sqlalchemy import SQLAlchemyAutoSchema

db = SQLAlchemy()
ma = Marshmallow()

### Models ###

class Machine(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    serial_number = db.Column(db.Text, unique=True)
    percent_complete = db.Column(db.Float)
    part_number_id = db.Column(db.Integer, db.ForeignKey('PartNumbers.id'))
    timestamp_created = db.Column(db.DateTime, default=datetime.utcnow)

class PartNumbers(db.Model):
    __tablename__ = 'PartNumbers'

    id = db.Column(db.Integer, primary_key=True)
    part_number = db.Column(db.Text, unique=True)
    description = db.Column(db.Text)
    machines = db.relationship('Machines', backref='machines', lazy='dynamic')


### Schemas ###

class PartNumbersSchema(SQLAlchemyAutoSchema):
    class Meta:
        model = PartNumbers
        include_fk = True

class MachineSchema(SQLAlchemyAutoSchema):
    class Meta:
        model = Machines
        include_relationships = True

__init__.py

from flask import Flask
from .models import db, ma
from .views import my_app

app = Flask(__name__)
db.init_app(app)
ma.init_app(app)
app.register_blueprint(my_app)

视图.py

from flask import Blueprint
from .models import db, Machines, MachineSchema, PartNumbers

my_app = Blueprint("my_app", __name__)

machines_schema = MachineSchema(many=True)

@my_app.route('/')
def home()
    machines = db.session.query(Machines).all()
    return machines_schema.dump(machines)
4

1 回答 1

1
  1. 在 Machine 类中定义 part_number:

    class Machine(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      serial_number = db.Column(db.Text, unique=True)
      percent_complete = db.Column(db.Float)
      part_number_id = db.Column(db.Integer, db.ForeignKey('PartNumbers.id'))
      timestamp_created = db.Column(db.DateTime, default=datetime.utcnow)
      part_numbers = db.relationship('PartNumbers')
    
  2. 您必须在 MachineSchema 中明确定义 part_numbers:

    from marshmallow_sqlalchemy import fields
    class MachineSchema(SQLAlchemyAutoSchema):
      class Meta:
        model = Machines
      part_numbers = fields.Nested(PartNumbersSchema)
    

我假设 part_numbers 是单个对象(1:1 关系)。否则,您必须将 many=True 添加到 part_numbers。

更多细节:https ://marshmallow.readthedocs.io/en/stable/nesting.html

[编辑以在评论中加入更正]

于 2020-03-17T16:47:43.273 回答