umongo icon indicating copy to clipboard operation
umongo copied to clipboard

Method Fields and Context?

Open Makiyu-py opened this issue 3 years ago • 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.

Makiyu-py avatar Jul 21 '21 01:07 Makiyu-py