django-ninja
django-ninja copied to clipboard
Error when trying to do django runserver -> pydantic.errors.PydanticSchemaGenerationError
Describe the bug I have django models
#models.py
class ClassDetail(models.Model):
class_name = models.CharField(max_length=50)
def __str__(self):
return self.class_name
class Student(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
full_name = models.CharField(max_length=100)
classs = models.ForeignKey(ClassDetail, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now=True)
updated_at = models.DateTimeField(auto_now_add=True)
def save(self, *args, **kwargs):
self.full_name = self.first_name + " " + self.full_name
super(Student, self).save(*args, **kwargs)
and Ninja Schema
#Schema.py
from ninja import ModelSchema
from .models import ClassDetail, Student
class ClassSchema(ModelSchema):
class Config:
model = ClassDetail
model_fields = "__all__"
arbitrary_types_allowed=True
class StudentSchema(ModelSchema):
classs: ClassSchema
class Config:
model = Student
model_fields = '__all__'
and api routes
#api.py
from typing import List
from ninja import Router
from .models import Student, ClassDetail
from .schema import ClassSchema, StudentSchema
router = Router()
@router.post('/class', response=ClassSchema)
def create_clas(request, payload=ClassSchema):
clas = ClassDetail.objects.create(**payload.dict())
return clas
@router.get('/student', response=List[StudentSchema])
def get_students(request):
schema = Student.objects.all()
return schema
@router.post('/student', response=StudentSchema)
def create_student(request, payload: StudentSchema):
stu = Student.objects.create(**payload.dict())
return stu
when trying to run the server i am getting this error
pydantic.errors.PydanticSchemaGenerationError: Unable to generate pydantic-core schema for <class 'ninja.orm.metaclass.ModelSchemaMetaclass'>. Set `arbitrary_types_allowed=True` in the model_config to ignore this error or implement `__get_pydantic_core_schema__` on your type to fully support it.
If you got this error by calling handler(<some type>) within `__get_pydantic_core_schema__` then you likely need to call `handler.generate_schema(<some type>)` since we do not call `__get_pydantic_core_schema__` on `<some type>` otherwise to avoid infinite recursion.
Versions
- Python version: [3.9]
- Django version: [5.0.4]
- Django-Ninja version: [1.1.0]
- Pydantic version: [2.7.1]
- pydantic core version: [2.18.2]
Missing return in save def method.
#models.py
class Student(models.Model):
[...]
def save(self, *args, **kwargs):
student = super(Student, self).save(*args, **kwargs)
student.full_name = f"{student.first_name} {student.last_name}"
student.save()
return student
Alternative:
#models.py
class Student(models.Model):
[...]
def save(self, *args, **kwargs):
student = super(Student, self).save(*args, **kwargs)
student.full_name = student.first_name + " " + student.full_name
student.save()
return student
In your original code you have self.full_name when it should be self.last_name
I am getting this same error whenever I use one Schema in another I am completely blocked too.
@nharrisanalyst @93Deepak
change:
def create_clas(request, payload=ClassSchema):
to:
def create_clas(request, payload: ClassSchema):