Scille/umongo

Method Fields and Context?

Opened this issue · 0 comments

Kind of like what marshmallow's context-aware serialization has:

class PersonSchema(Schema):
    id = fields.Integer()
    name = fields.Method("get_name")

    def get_name(self, person, context):
        if context.get("anonymize"):
            return "<anonymized>"
        return person.name


person = Person(name="Monty")
schema = PersonSchema()
schema.dump(person)  # {'id': 143, 'name': 'Monty'}

# In a different context, anonymize the name
schema.context["anonymize"] = True
schema.dump(person)  # {'id': 143, 'name': '<anonymized>'}

but with asynchronous methods (depending on the driver)

It would be very useful to have this feature here on uMongo too.