pydantic

How to return the average value using fastapi & pydantic

How to return the average value using fastapi & pydantic Question: I am new to fasts-api and python as I am a mobile developer. At the moment I managed to get this response from my API (please look at averageLevel which is an array at the moment): [ { "user_id": 139, "event_date": "2023-03-20T12:18:17", "public": 1, …

Total answers: 1

Is there a way to execute a function after a Pydantic model is initialized?

Is there a way to execute a function after a Pydantic model is initialized? Question: Im trying to always provide the hashed id: class MySchema(BaseModel): id: int hashed_id: str = None def __init__(self, **data: Any) -> None: super().__init__(**data) self.hash_id() def hash_id(self): self.hashed_id = encode(self.id) It doesn’t seem to do anything really. It outputs: { "id": …

Total answers: 2

Library for JSON serialization/deserialization of objects in Python

Library for JSON serialization/deserialization of objects in Python Question: I’m looking for a python library to help with serialization/deserialization of pretty complicated objects in Python. I am writing a library where the top level object is very roughly (using dataclass syntax) class ExampleProgram: component_1 : ComponentType widgets : Dict[str, WidgetType] foo : List[FooType] The key …

Total answers: 1

pydantic: BaseSettings with parametrized default values

pydantic: BaseSettings with parametrized default values Question: It appears one cannot create an __init__ function on pydantic BaseSettings superclasses. I am trying to achieve this (note; for demonstration, this code is not supported in pydantic): class MySettings(BaseSettings): def __init__(self, foo: str): self.p1 = f"{foo}_param_name1" # <- trying to achieve this self.p2 = f"{foo}_param_name2" # <- …

Total answers: 1

Pydantic nested setting objects load env variables from file

Pydantic nested setting objects load env variables from file Question: Using pydantic setting management, how can I load env variables on nested setting objects on a main settings class? In the code below, the sub_field env variable field doesn’t get loaded. field_one and field_two load fine. How can I load an environment file so the …

Total answers: 1

Pydantic BaseModel schema and instance serialization/deserialization not working

Pydantic BaseModel schema and instance serialization/deserialization not working Question: I am attempting to serialize a Pydantic model schema and then deserialize it in another script. The serialization process is working as expected, and it has created two JSON files: model.json and data.json. In test_save.py, I defined the MainModel schema and then serialized it along with …

Total answers: 2

Validate Pydantic dynamic float enum by name with OpenAPI description

Validate Pydantic dynamic float enum by name with OpenAPI description Question: Following on from this question and this discussion I am now trying to create a Pydantic BaseModel that has a field with a float Enum that is created dynamically and is validated by name. (Down the track I will probably want to use Decimal …

Total answers: 3

Circular import with beanie ODM

Circular import with beanie ODM Question: I need to use a cross-reference in my MongoDB schema. I use beanie as ODM. Here is my models: entity.py from beanie import Document class Entity(Document): path: List["Folder"] = [] folder.py from entity import Entity class Folder(Entity) pass init_beanie.py import beanie from motor.motor_asyncio import AsyncIOMotorClient from entity import Entity …

Total answers: 1

Fast API getting an error while post api in my bills model

Fast API getting an error while post api in my bills model Question: So, i am using fast api and I am trying to add added_on in my taxbill schema here is my bill model class TaxBillModel(Base): __tablename__ = "taxbill" id = Column(Integer, primary_key=True, index=True) bill_no = Column(Integer, index=True) amount = Column(Integer, nullable=False) about = …

Total answers: 1

How to subclass a frozen dataclass using pydantic

How to subclass a frozen dataclass Question: I have inherited the Customer dataclass. This identifies a customer in the customer DB table. Customer is used to produce summary statistics for transactions pertaining to a given customer. It is hashable, hence frozen. I require a SpecialCustomer (a subclass of Customer) it has an extra property: special_property. …

Total answers: 1