0

I have the following Python class and Marshmallow schema definitions:

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

As the sample hopefully illustrates, I'd like to have a data model in which book objects contain references to authors (instead of just author ids), but which would be serialized into author ids.

Obviously, serializing is not a problem, but when deserializing, I'd need to access the already deserialized contents of the authors list. I can't quite figure how to do that with Marshmallow. Is it even possible? Of course, in this case I could instantiate Book objects with numeric author ids and do a @post_load operation in LibrarySchema to replace the author id with a reference, but that feels dirty and clunky to me. Please help. :-)

mairas
  • 13
  • 5

1 Answers1

0

Linking data in post_load is exactly how you should do it.

Maxim Kulkin
  • 2,698
  • 22
  • 11