Closed b-simjoo closed 4 weeks ago
For now I added a pydantic validator on my db model (db.Employee
) that fixes the issue
class Employee(Document):
id:str
department:Optional[Department]
@field_validator("department")
@classmethod
def validate_backlink(cls, v):
if isinstance(v, BackLink):
return None
return v
This issue is stale because it has been open 30 days with no activity.
This issue was closed because it has been stalled for 14 days with no activity.
Describe the bug Hi, I'm using FastAPI and Beanie and I have to set of models, one using Beanie for database and other one for API schema. The database has two documents
Employee
andDepartment
that are linked :On the other side I have proper Models for both:
Since I'm using FastAPI, when I return a data instance, it tries to convert from db to schema (using pydantic method
Model.model_validate(v,from_attributes=True)
).Everything works just fine until I try to get an employee that no Department is linked to. Then
Employee.department
field is an emptyBackLink
that is not validate or processable for pydantic so it raises a Validation Error:Expected behavior for fields of type
Optional[BackLink[ADoc]]
set/returnNone
when there is no back-link.