[Python] How to update pydantic model from dictionary?

Discussão em 'Python' iniciado por Stack, Maio 13, 2025.

  1. Stack

    Stack Membro Participativo

    I have a pydantic model:

    from pydantic import BaseModel

    class MyModel(BaseModel):
    value : str = 'Some value'


    And I need to update this model using a dictionary (not create).

    I tried updating the model using class.__dict__, but after updating that's just a dictionary, not model values.

    So I need something like this:

    from pydantic import BaseModel

    class MyModel(BaseModel):
    value : str = 'Some value'

    model = MyModel()
    update_model(model = model, data = {'value' : 'Bla bla bla'}) # here must be a function to update model :D

    print(model.__dict__)
    {'value' : 'Bla bla bla'}

    Continue reading...

Compartilhe esta Página