Flask Marshmallow/SqlAlchemy:序列化多对多关系
Flask Marshmallow/SqlAlchemy: Serializing many-to-many relationships
我正在使用 Flask、flask-sqlalchemy 和 flask-marshmallow 构建一个小型 REST api。对于某些请求,我想 return 一个 json 由我的 sqlalchemy 对象组成的序列化响应。但是,在使用多对多关系/辅助表时,我无法使序列化与急切加载的 sqlalchemy 对象一起使用。
这是一个简单的例子,或多或少 copy/pasted 来自 flask-marshmallow 文档:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
from sqlalchemy.orm import joinedload
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://'
# Order matters: Initialize SQLAlchemy before Marshmallow
db = SQLAlchemy(app)
ma = Marshmallow(app)
secondary_foo = db.Table('secondary_foo',
db.Column('author_id', db.Integer, db.ForeignKey('author.id')),
db.Column('book_id', db.Integer, db.ForeignKey('book.id')))
class Author(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255))
class Book(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(255))
authors = db.relationship('Author', secondary="secondary_foo", backref='books', lazy="joined")
class AuthorSchema(ma.ModelSchema):
class Meta:
model = Author
class BookSchema(ma.ModelSchema):
#authors = ma.Nested(AuthorSchema) <-- Doesn't work, authors will be serialized to empty json object, instead of list of ids
class Meta:
model = Book
db.drop_all()
db.create_all()
author_schema = AuthorSchema()
book_schema = BookSchema()
author = Author(name='Chuck Paluhniuk')
book = Book(title='Fight Club')
book.authors.append(author)
db.session.add(author)
db.session.add(book)
db.session.commit()
s = BookSchema(many=True)
基于上面的代码,我可以热切地加载书籍并获得作者对象。但是在序列化深层对象时,序列化为一个 ID 列表:
print(Book.query.filter(1==1).options(joinedload('authors')).all()[0].authors)
//--> [<__main__.Author object at 0x1043a0dd8>]
print(s.dump(Book.query.filter(1==1).options(joinedload('authors')).all()).data)
//--> [{'authors': [1], 'title': 'Fight Club', 'id': 1}]
这是我想要的结果:
print(s.dump(Book.query.filter(1==1).options(joinedload('authors')).all()).data)
//--> [{'authors': [{'name':'Chuck Paluhniuk', 'id':'1'}], 'title': 'Fight Club', 'id': 1}]
我该怎么做?
在您的 BookSchema
中,您需要像之前那样添加一个 Nested
authors
字段(但已注释掉),但您需要指定它是使用 many
keyword argument.
的列表
class BookSchema(ma.ModelSchema):
# A list of author objects
authors = ma.Nested(AuthorSchema, many=True)
class Meta:
model = Book
我正在使用 Flask、flask-sqlalchemy 和 flask-marshmallow 构建一个小型 REST api。对于某些请求,我想 return 一个 json 由我的 sqlalchemy 对象组成的序列化响应。但是,在使用多对多关系/辅助表时,我无法使序列化与急切加载的 sqlalchemy 对象一起使用。
这是一个简单的例子,或多或少 copy/pasted 来自 flask-marshmallow 文档:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
from sqlalchemy.orm import joinedload
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://'
# Order matters: Initialize SQLAlchemy before Marshmallow
db = SQLAlchemy(app)
ma = Marshmallow(app)
secondary_foo = db.Table('secondary_foo',
db.Column('author_id', db.Integer, db.ForeignKey('author.id')),
db.Column('book_id', db.Integer, db.ForeignKey('book.id')))
class Author(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255))
class Book(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(255))
authors = db.relationship('Author', secondary="secondary_foo", backref='books', lazy="joined")
class AuthorSchema(ma.ModelSchema):
class Meta:
model = Author
class BookSchema(ma.ModelSchema):
#authors = ma.Nested(AuthorSchema) <-- Doesn't work, authors will be serialized to empty json object, instead of list of ids
class Meta:
model = Book
db.drop_all()
db.create_all()
author_schema = AuthorSchema()
book_schema = BookSchema()
author = Author(name='Chuck Paluhniuk')
book = Book(title='Fight Club')
book.authors.append(author)
db.session.add(author)
db.session.add(book)
db.session.commit()
s = BookSchema(many=True)
基于上面的代码,我可以热切地加载书籍并获得作者对象。但是在序列化深层对象时,序列化为一个 ID 列表:
print(Book.query.filter(1==1).options(joinedload('authors')).all()[0].authors)
//--> [<__main__.Author object at 0x1043a0dd8>]
print(s.dump(Book.query.filter(1==1).options(joinedload('authors')).all()).data)
//--> [{'authors': [1], 'title': 'Fight Club', 'id': 1}]
这是我想要的结果:
print(s.dump(Book.query.filter(1==1).options(joinedload('authors')).all()).data)
//--> [{'authors': [{'name':'Chuck Paluhniuk', 'id':'1'}], 'title': 'Fight Club', 'id': 1}]
我该怎么做?
在您的 BookSchema
中,您需要像之前那样添加一个 Nested
authors
字段(但已注释掉),但您需要指定它是使用 many
keyword argument.
class BookSchema(ma.ModelSchema):
# A list of author objects
authors = ma.Nested(AuthorSchema, many=True)
class Meta:
model = Book