1

I am wanting a multi level nested serializer. Essentially every school has a list of students and every student has a list of grades. It is outputting this

{"id":1,"name":"Sierra Grande Elem/Junior High"}

and looks like it is not including any of the nests I created.

Here is my models.py

class School(models.Model):
    name = models.TextField(max_length = 150, blank=True)

class Student(models.Model):
    current_school = models.ForeignKey(
        "School",
        null = True,
        on_delete = models.PROTECT,
    )
    first_name = models.CharField(max_length = 35, blank=True)
    last_name = models.CharField(max_length = 35, blank=True)


class Grade(models.Model):
    student = models.ForeignKey(
        "Student",
        null = True,
        on_delete = models.PROTECT,
    )
    course = models.ForeignKey(
        "Course",
        null = True,
        on_delete = models.PROTECT,
    )
    grade = models.FloatField(null = True)

Here is my serializers.py

class NestedSchoolSerializer(serializers.ModelSerializer):
    class Meta:
        model = School
        fields = ("id","name")

class NestedStudentSerializer(serializers.ModelSerializer):
    school_set = NestedSchoolSerializer(many = True, read_only = True)
    class Meta:
        model = Student
        fields = ("school_set","id","first_name","last_name")


class NestedGradeSerializer(serializers.ModelSerializer):
    student_set = NestedStudentSerializer(many = True, read_only = True)
    class Meta: 
        model = Grade
        fields = ("student_set","id","course","grade")

Here is my views.py file:

class SchoolInfo(generics.RetrieveAPIView):
    queryset = School.objects.all()
    serializer_class = NestedSchoolSerializer  

Here is my url.py file:

urlpatterns = [
    path('school/grade/<int:pk>/', views.SchoolInfo.as_view()),
  ]

Any help is much appreciated! New to Django and have been trouble shooting this for quite some time now. What part of my code do I need to change?

1 Answer 1

1

Welcome to Stack Overflow! Looks like you're on the right track, but you have the nesting inverted. Your NestedSchoolSerializer doesn't know anything about the students and the NestedStudentSerializer doesn't know about the grades. There may be other issues, but this is the first one to jump out at me. Try something like this instead:

class NestedGradeSerializer(serializers.ModelSerializer):
    class Meta: 
        model = Grade
        fields = ("id","course","grade")

class NestedStudentSerializer(serializers.ModelSerializer):
    grade_set = NestedGradeSerializer(many = True, read_only = True)
    class Meta:
        model = Student
        fields = ("grade_set","id","first_name","last_name")


class NestedSchoolSerializer(serializers.ModelSerializer):
    student_set = NestedStudentSerializer(many = True, read_only = True)
    class Meta:
        model = School
        fields = ("id", "name", "student_set")

One way to know how to set these things up is that for the most part the fields in your serializer should map to fields in the same model (if you're using a ModelSerializer).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.