1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Is there any post_load in pydantic?

Discussão em 'Python' iniciado por Stack, Setembro 11, 2025.

  1. Stack

    Stack Membro Participativo

    Previously I used the marshmallow library with the Flask. Some time ago I have tried FastAPI with Pydantic. At first glance pydantic seems similar to masrhmallow but on closer inspection they differ. And for me the main difference between them is post_load methods which are from marshmallow. I can't find any analogs for it in pydantic.

    post_load is decorator for post-processing methods. Using it I can handle return object on my own, can do whatever I want:

    class ProductSchema(Schema):
    alias = fields.Str()
    category = fields.Str()
    brand = fields.Str()

    @post_load
    def check_alias(self, params, **kwargs):
    """One of the fields must be filled"""
    if not any([params.get('alias'), params.get('category'), params.get('brand')]):
    raise ValidationError('No alias provided', field='alias')
    return params


    Besides it used not only for validation. Code example is just for visual understanding, do not analyze it, I have just invented it.

    So my question is: is there any analog for post_load in pydantic?

    Continue reading...

Compartilhe esta Página