pydantic

by_alias parameter on model_dump() is being ignored

by_alias parameter on model_dump() is being ignored Question: In the following code, we see that the field id is indeed created with the alias of user_id when we print the model_fields. However, when I then call model_dump(alias=True), the returned dict has an id key, but does not have a user_id key as I am expecting. …

Total answers: 2

Condense Pydantic Validators into Class Annotations

Condense Pydantic Validators into Class Annotations Question: Looking to create a datastructure in Python that condenses Pydantic’s validators into oneline arguments: ie. from this from pydantic import BaseModel, validator class MyClass(BaseModel): a: str b: str c: str @validator(‘b’) def calculate_b(cls, v, values): return values[‘a’] + v @validator(‘c’) def calculate_c(cls, v, values): return values[‘a’] + values[‘b’] …

Total answers: 1

Migrate PostgresDsn.build from pydentic v1 to pydantic v2

Migrate PostgresDsn.build from pydentic v1 to pydantic v2 Question: I have simple Config class from FastAPI tutorial. But it seems like it uses old pydantic version. I run my code with pydantic v2 version and get a several errors. I fix almost all of them, but the last one I cannot fix yet. This is …

Total answers: 5

Can I return 400 error instead of 422 error

Can I return 400 error instead of 422 error Question: I validate data using Pydantic schema in my FastAPI project and if it is not ok it returns 422. Can I change it to 400? Asked By: Konstantinos || Source Answers: Yes, you can. For example, you can apply the next exception handler: from fastapi …

Total answers: 1

SQL query to create a nested pydantic structure

SQL query to create a nested pydantic structure Question: While working with SQLAlchemy 2.x and FastAPI, I ran into the problem of creating a nested structure for pydantic serialization. The specific essence of the problem lies in the implementation of the sql query. The fact is that the received data comes as a single tuple …

Total answers: 1

How does `mypy` know the signature of the pydantic model?

How does `mypy` know the signature of the pydantic model? Question: How does mypy know the signature of the pydantic model in this manner? from pydantic import BaseModel class Model(BaseModel): a: int Model(a=’asd’) # error: Argument "a" to "Model" has incompatible type "str"; expected "int" How can pydantic BaseModel‘s metaclass change the __init__ signature? Asked …

Total answers: 1

How to get Pydantic model types?

How to get Pydantic model types? Question: Consider the following model: from pydantic import BaseModel class Cirle(BaseModel): radius: int pi = 3.14 If I run the following code, I can see the fields of this model: print(Circle.__fields__) # Output: { ‘radius’: ModelField(name=’radius’, type=int, required=True), ‘pi’: ModelField(name=’pi’, type=float, required=False, default=3.14) } The question is: how can …

Total answers: 1

Convert non-tabular, comma-separated data to pydantic

Convert non-tabular, comma-separated data to pydantic Question: I have a "special" csv file in the following format: A;ItemText;1;2 B;1;1.23,99 B;2;9.52,100 C;false I would like to convert this data into pydantic models. Currently I subclassed the FieldInfo class: class CSVFieldInfo(FieldInfo): def __init__(self, **kwargs: Any): self.position = kwargs["position"] if not isinstance(self.position, int): raise ValueError("Position should be integer, …

Total answers: 1

Generate SQLAlchemy models from Pydantic models

Generate SQLAlchemy models from Pydantic models Question: So I have this large JSON dataset with a huge number of properties (as well as nested objects). I have used datamodel-code-generator to build pydantic models. The JSON data has been imported into pydantic models (with nested models). Now I need to push them into the database. How …

Total answers: 1