phoenix97
phoenix97

Reputation: 209

How to add pydantic field validation for django model?

So, I have a django model, let's say, it's field division is as follows:

class ModelA(models.Model):
    f1 = models.CharField(max_length=127)
    f2 = models.CharField(max_length=127)

I want to create a pydantic class, with a field type containing list of these models. Consider the following example:

class TestPydanticClass(BaseModel):
    model_a_values: List[ModelA]
    f4: int

When I am trying to create the TestPydanticClass object, but it throws the following error:

RuntimeError: no validator found see `arbitrary_types_allowed` in Config

Based upon this, I added arbitrary_types_allowed as True, under the Config class for the model, and it still throws an error. Can someone pls suggest a better method of achieving this?

Upvotes: 2

Views: 8976

Answers (1)

Francisco
Francisco

Reputation: 656

Did you set this way, inside the BaseModel?

class TestPydanticClass(BaseModel):
    model_a_values: List[ModelA]
    f4: int

    class Config:
        arbitrary_types_allowed = True

Upvotes: 7

Related Questions