Skip to content
This repository has been archived by the owner on Jan 19, 2024. It is now read-only.

RelatedManager and Nested field #99

Closed
davidzwa opened this issue Jul 5, 2019 · 0 comments
Closed

RelatedManager and Nested field #99

davidzwa opened this issue Jul 5, 2019 · 0 comments

Comments

@davidzwa
Copy link
Contributor

davidzwa commented Jul 5, 2019

Made a small snippet for serializing relatedmanager objects (lazily sadly).

from marshmallow import ValidationError
from marshmallow.fields import Nested


class RelatedNested(Nested):

    def _serialize(self, nested_obj, attr, obj, **kwargs):
        # Load up the schema first. This allows a RegistryError to be raised
        # if an invalid schema name was passed
        schema = self.schema
        if nested_obj is None:
            return None
        try:
            return schema.dump(getattr(obj, attr).all(), many=self.many)
        except ValidationError as exc:
            raise ValidationError(exc.messages, valid_data=exc.valid_data) from exc

Any advice on whether using the obj directly is the right way?

@davidzwa davidzwa changed the title RelatedManager and & FieldNested RelatedManager and FieldNested Jul 5, 2019
@davidzwa davidzwa changed the title RelatedManager and FieldNested RelatedManager and Nested field Jul 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant