从 Python Marshmallow 中的兄弟字段访问反序列化数据?
Accessing deserialized data from sibling fields in Python Marshmallow?
我有以下 Python class 和 Marshmallow 架构定义:
from marshmallow import Schema, fields, post_load
class Author:
def __init__(self, id, name):
self.id = id
self.name = name
class Book:
def __init__(self, id, author, name):
self.id = id
assert isinstance(author, Author)
self.author = author
self.name = name
class Library:
def __init__(self, authors, books):
self.authors = authors
self.books = books
class AuthorSchema(Schema):
id = fields.Int()
name = fields.Str()
@post_load
def make_obj(self, data):
return Author(**data)
class BookSchema(Schema):
id = fields.Int()
author_id = fields.Method('get_id', 'get_author', attribute="author")
name = fields.Str()
@post_load
def make_obj(self, data):
return Book(**data)
def get_id(self, obj):
return obj.author.id
def get_author(self, value):
return [a for a in authors if a.id == value][0]
class LibrarySchema(Schema):
authors = fields.List(fields.Nested(AuthorSchema()))
books = fields.List(fields.Nested(BookSchema()))
@post_load
def make_obj(self, data):
return Library(**data)
# preserve ordering of fields
class Meta:
ordered = True
def test_author_referencing():
author1 = Author(1, "Astrid Lindgren")
author2 = Author(2, "Tove Jansson")
book1 = Book(11, author1, "The Brothers Lionheart")
book2 = Book(12, author2, "Comet in Moominland")
library = Library(authors=[author1, author2], books=[book1, book2])
schema = LibrarySchema(strict=True)
library_dict = schema.dump(library).data
library2 = schema.load(library_dict).data
如示例所示,我想要一个数据模型,其中书籍对象包含对作者的引用(而不仅仅是作者 ID),但会序列化为作者 ID。
很明显,序列化是没有问题的,但是反序列化的时候,
我需要访问作者列表中已经反序列化的内容。我不太清楚如何用 Marshmallow 做到这一点。有可能吗?当然,在这种情况下,我可以用数字作者 ID 实例化 Book
对象,并在 LibrarySchema
中执行 @post_load
操作以用引用替换作者 ID,但这感觉又脏又笨重我。请帮忙。 :-)
在 post_load 中链接数据正是您应该做的。
我有以下 Python class 和 Marshmallow 架构定义:
from marshmallow import Schema, fields, post_load
class Author:
def __init__(self, id, name):
self.id = id
self.name = name
class Book:
def __init__(self, id, author, name):
self.id = id
assert isinstance(author, Author)
self.author = author
self.name = name
class Library:
def __init__(self, authors, books):
self.authors = authors
self.books = books
class AuthorSchema(Schema):
id = fields.Int()
name = fields.Str()
@post_load
def make_obj(self, data):
return Author(**data)
class BookSchema(Schema):
id = fields.Int()
author_id = fields.Method('get_id', 'get_author', attribute="author")
name = fields.Str()
@post_load
def make_obj(self, data):
return Book(**data)
def get_id(self, obj):
return obj.author.id
def get_author(self, value):
return [a for a in authors if a.id == value][0]
class LibrarySchema(Schema):
authors = fields.List(fields.Nested(AuthorSchema()))
books = fields.List(fields.Nested(BookSchema()))
@post_load
def make_obj(self, data):
return Library(**data)
# preserve ordering of fields
class Meta:
ordered = True
def test_author_referencing():
author1 = Author(1, "Astrid Lindgren")
author2 = Author(2, "Tove Jansson")
book1 = Book(11, author1, "The Brothers Lionheart")
book2 = Book(12, author2, "Comet in Moominland")
library = Library(authors=[author1, author2], books=[book1, book2])
schema = LibrarySchema(strict=True)
library_dict = schema.dump(library).data
library2 = schema.load(library_dict).data
如示例所示,我想要一个数据模型,其中书籍对象包含对作者的引用(而不仅仅是作者 ID),但会序列化为作者 ID。
很明显,序列化是没有问题的,但是反序列化的时候,
我需要访问作者列表中已经反序列化的内容。我不太清楚如何用 Marshmallow 做到这一点。有可能吗?当然,在这种情况下,我可以用数字作者 ID 实例化 Book
对象,并在 LibrarySchema
中执行 @post_load
操作以用引用替换作者 ID,但这感觉又脏又笨重我。请帮忙。 :-)
在 post_load 中链接数据正是您应该做的。