Open migratis opened 2 years ago
@vitalik could this be something django-countries has to implement on their end?
Im getting the same problem when adding a CountryField() from django-countries (added to custom user model) If you use this schema in a response you'll get an error:
File "pydantic/main.py", line 579, in pydantic.main.BaseModel.from_orm
pydantic.error_wrappers.ValidationError: 1 validation error for NinjaResponseSchema
response -> country
str type expected (type=type_error.str)
Minimal example:
from ninja import ModelSchema
from django.contrib.auth import get_user_model
class UserOutSchema(ModelSchema):
class Config:
model = get_user_model()
model_fields = [
"email",
"country",
]
And if you add the Country type from django_countries to the schema it will throw an error that an "Object of type Country is not JSON serializable":
from django_countries.fields import Country
class UserOutSchema(ModelSchema):
country: Country
class Config:
arbitrary_types_allowed = True
model = get_user_model()
model_fields = [
"email",
"country",
]
The Country field is not just a string but a Country object with multiple attributes. Hence the user model is nested. You need to define a Country schema first which then can be used in the User schema like it's stated in the docs for overriding fields: https://django-ninja.dev/guides/response/django-pydantic/?h=modelschema#modelschema
A model using the CountryField() from django-countries if it is defined with ModelSchema to be used as a response, drive to this error :
here is how I define the schema and use it: