Trying to add a pydantic model to a set gives unhashable error

I have the following code from pydantic import BaseModel class User(BaseModel): id: int name = "Jane Doe" def add_user(user: User): a = set() a.add(user) return a add_user(User(id=1)) When I run this, I get the following error: TypeError: unhashable type: ‘User’ Is there a way this issue can be solved? >Solution : You just need to… Read More Trying to add a pydantic model to a set gives unhashable error

Unpacking str dict value into datetime attribute of an object in a nice way

I have a pydantic class like class MyClass(BaseModel): base: str success: bool date: datetime I also have a dict like dict = { "base": "abc", "success": True, "date": "2023-04-30" } So I want to do something like obj = MyClass(**dict) but also convert str "date" from dict into datetime "date" of the obj automatically How… Read More Unpacking str dict value into datetime attribute of an object in a nice way

How to call POST API using body prams in angular [ raise 422 error (Unprocessable Entity)]

I’m struggling to use Post API using FastAPI , Angular while using HttpClient.post Function the issue with receiving the prams in the backend FastAPI no seeing the prams and raise 422 (Unprocessable Entity) Mybackend API CODE (FastAPI-Python): from typing import Optional from pydantic import BaseModel class UsersLoginRequest(BaseModel): username: str password: str @users_router.post("/login") def login(body: UsersLoginRequest):… Read More How to call POST API using body prams in angular [ raise 422 error (Unprocessable Entity)]

Pydantic: Storing attributes that are not part of the model (equivalent of JsonExtensionData in C#)

I’m using pydantic to load data from a json structure into a MyModel instance (see example below). Sometimes the JSON data come with extra keys which are not defined in the class MyModel. Anyways I’d like have those data accessible in the deserialized MyModel instance somehow. Minimal Working Example: from pydantic import BaseModel class MyModel(BaseModel):… Read More Pydantic: Storing attributes that are not part of the model (equivalent of JsonExtensionData in C#)

How to merge multiple models into one common schema in Fastapi

I recently started studyingfastapi and can’t serialize response schema. Got two model object, but I could not bring it to a specific scheme. Without using response_model, I just get the following response: { "OcProduct": { "viewed": 617, "product_id": 160, "model": "name", "stock_status_id": 6, "image": "catalog/product/test.jpg", "status": 1 }, "OcStockStatu": { "name": "Out Of Stock", "language_id":… Read More How to merge multiple models into one common schema in Fastapi

How to set swagger ui to use list fields in query parameters with FastAPI

When making an app that uses dependency injection with a list field, the parameter automatically goes to the request body in SwaggerUI: from fastapi import FastAPI, Query, Depends import uvicorn from pydantic import BaseModel, Field from typing import List class QueryParams(BaseModel): name: str = Field(…) ages: List[int] = Field([]) app = FastAPI() @app.get("/test") def test(query:… Read More How to set swagger ui to use list fields in query parameters with FastAPI

pydantic – json keys are not valid python field names

I have a json with keys containing characters that a python field name can’t contain: { "mlflow.source.git.commit": "fbe812fe", "other.key": "other.value" } How to use pydantic to parse such a json? I’d like to give it an alias and actual key name, like class Tags(pydantic.BaseModel): commit = field(key="mlflow.source.git.commit", type=str) >Solution : This is possible using pydantic.Field(alias="…"):… Read More pydantic – json keys are not valid python field names