marshmallow sqlalchemy中有没有方法在序列化/反序列化zng Bar时为嵌套(foos)指定只加载或只转储字段吗?在
class FooSchema(BaseSchema):
class Meta(BaseSchema.Meta):
model = Foo
fields = ('id', 'name', 'date', 'clients')
class BarSchema(BaseSchema):
class Meta(BaseSchema.Meta):
model = Bar
fields('id',)
foos = Nested(FooSchema, many=True, only=('id', 'name'))
# is there a way to add to foos field something like load_only=('id',)
# without changing FooSchema?
我建议不要在
Nested
关系的定义中指定only
。使用exclude
来防止循环引用,并在每次序列化时显式指定要only
的字段。在另外,您通常不需要指定
fields
-marshmallow-sqlalchemy
为大多数字段免费提供了这个选项。下面是我如何重构上述内容:相关问题 更多 >
编程相关推荐