Menu Close

How to include related model fields using Django Rest Framework?

To include related model fields using Django Rest Framework, we can set the depth property in the serializer class.

For instance, we write

class ClassroomSerializer(serializers.ModelSerializer):
    class Meta:
        model = Classroom
        depth = 1

to set the depth field in the Meta class in the ClassroomSerializer to 1 to include the model fields directly related to Classroom.

Posted in Python, Python Answers