在 marshmallow-mongoengine 中使用参考字段
Working with reference fields in marshmallow-mongoengine
如何在 marshmallow_mongoengine 中取消引用 ReferenceFields?例如,dump_data = author_schema.dump(author).data
导致 '5578726b7a58012298a5a7e2'
而不是更有用的响应 {title='Fight Club', author=author}
.
from marshmallow_mongoengine import ModelSchema
class AuthorSchema(ModelSchema):
class Meta:
model = Author
class BookSchema(ModelSchema):
class Meta:
model = Book
author_schema = AuthorSchema()
author = Author(name='Chuck Paluhniuk').save()
book = Book(title='Fight Club', author=author).save()
dump_data = author_schema.dump(author).data
# {'id': 1, 'name': 'Chuck Paluhniuk', 'books': ['5578726b7a58012298a5a7e2']}
author_schema.load(dump_data).data
# <Author(name='Chuck Paluhniuk')>
from marshmallow.fields import Nested
class AuthorSchema(ModelSchema):
class Meta:
model = Author
books = Nested("BookSchema",many=True)
如何在 marshmallow_mongoengine 中取消引用 ReferenceFields?例如,dump_data = author_schema.dump(author).data
导致 '5578726b7a58012298a5a7e2'
而不是更有用的响应 {title='Fight Club', author=author}
.
from marshmallow_mongoengine import ModelSchema
class AuthorSchema(ModelSchema):
class Meta:
model = Author
class BookSchema(ModelSchema):
class Meta:
model = Book
author_schema = AuthorSchema()
author = Author(name='Chuck Paluhniuk').save()
book = Book(title='Fight Club', author=author).save()
dump_data = author_schema.dump(author).data
# {'id': 1, 'name': 'Chuck Paluhniuk', 'books': ['5578726b7a58012298a5a7e2']}
author_schema.load(dump_data).data
# <Author(name='Chuck Paluhniuk')>
from marshmallow.fields import Nested
class AuthorSchema(ModelSchema):
class Meta:
model = Author
books = Nested("BookSchema",many=True)