I am trying to create the pydantic model for the post Api but i am getting the following error:
Input should be a valid dictionary or object to extract fields from
Could anyone please help me regarding this?
from typing import List
from fastapi import UploadFile, File, Form, Depends
from pydantic import BaseModel, Field
class Types(BaseModel):
categoryid: str = Field()
categoryname: str = Field()
subcategoryid: str = Field()
subcategoryname: str = Field()
class Config:
from_attributes = True
populate_by_name = True
arbitrary_types_allowed = False
class Availability(BaseModel):
quantity: int = Form(...)
size: str = Form(...)
price: str = Form(...)
class Config:
from_attributes = True
populate_by_name = True
arbitrary_types_allowed = False
class ProductUnits(BaseModel):
color: str = Form(...)
image: str = Form(...)
# availability: List[Availability] = Depends()
class Product(BaseModel):
# productname: Optional[str] = None
# minorder: Optional[int] = None
# producttype: Types = Depends()
# productdetails: Optional[str] = None
productunits: List[ProductUnits]
# shippingtime: Optional[str] = None
# cod: Optional[bool] = None
# codminimumamt: Optional[int] = None
# returnpolicy: Optional[str] = None
productimages: list[UploadFile] = File(...)
Router
from fastapi import APIRouter, Request, status, Depends
from src.products.models import Product
from src.utils.statuscode import statuscode
productrouter = APIRouter()
@productrouter.post("/", response_description="Create a products", status_code=status.HTTP_201_CREATED,
)
async def create_product(request: Request, product: Product = Depends()):
print(product)
return statuscode(status=True, message='Product added Successfully')
Error Track
{
"detail": [
{
"type": "model_attributes_type",
"loc": [
"body",
"productunits",
0
],
"msg": "Input should be a valid dictionary or object to extract fields from",
"input": "{\r\n \"color\": \"string\",\r\n \"image\": \"string\"\r\n}",
"url": "https://errors.pydantic.dev/2.4/v/model_attributes_type"
}
]
}
I had tried to change the typing values and base model many more but i able achieve the expected output
Please help me understand whats the issue causing that error in the pydantic when i try to run that api