Nested Relationship
A nested relationship makes it possible to embed the referred entity. If we look at the case of EmployeeTask, it refers to the Employee. Let’s serialize the EmployeeTask using a nested relationship.
You can use the below code to refer to the employee field in the EmployeeTaskSerializer:
employee = EmployeeSerializer(read_only=True)
You can replace the EmployeeTaskSerializer with the below code:
Python3
class EmployeeTaskSerializer(serializers.HyperlinkedModelSerializer): url = serializers.HyperlinkedIdentityField( view_name = 'employeetask-detail' , lookup_field = 'pk' ) employee = EmployeeSerializer(read_only = True ) class Meta: model = EmployeeTask fields = '__all__' |
Let’s retrieve an EmployeeTask entry. The HTTPie command is
http GET :8000/task/1/
Output
HTTP/1.1 200 OK Content-Length: 569 Content-Type: application/json Date: Fri, 22 Jan 2021 16:33:51 GMT Referrer-Policy: same-origin Server: WSGIServer/0.2 CPython/3.7.5 X-Content-Type-Options: nosniff X-Frame-Options: DENY { "created_date": "2021-01-21T09:42:39.792788Z", "deadline": "2021-01-25T00:00:00Z", "employee": { "designation": "Software Engineer", "emp_id": 128, "gender": "M", "name": "Mathew A", "pk": 8, "tasks": [ "http://localhost:8000/task/2/", "http://localhost:8000/task/1/", "http://localhost:8000/task/6/", "http://localhost:8000/task/7/" ], "url": "http://localhost:8000/employees/8/" }, "task_desc": "Write a Python program to interchange first and last element in a list", "task_name": "Interchange first and last elements in a list", "url": "http://localhost:8000/task/1/" }
Sharing the command prompt screenshot for your reference
You can notice that the employee field displays the complete employee details rather than just the primary key or slug.
"employee": { "designation": "Software Engineer", "emp_id": 128, "gender": "M", "name": "Mathew A", "pk": 8, "tasks": [ "http://localhost:8000/task/2/", "http://localhost:8000/task/1/", "http://localhost:8000/task/6/", "http://localhost:8000/task/7/" ], "url": "http://localhost:8000/employees/8/" },
In this section, we explored various types of relational fields provided by DRF serializer relations. We understood how each relational fields represent the relationship with the target. The PrimaryKeyRelatedField represents the target of the relationship using its primary key whereas, the StringRelatedField uses the __str__ method of the target. In the case of SlugRelatedField, it represents the target of the relationship using a target field. Finally, the HyperlinkedIdentityField is used as an identity relationship, and the HyperlinkedRealtedField represents the target of the relationship using a hyperlink.
Serializer Relations – Django REST Framework
Serialization is one of the most important concepts in RESTful Webservices. It facilitates the conversion of complex data (such as model instances) to native Python data types that can be rendered using JSON, XML, or other content types. In Django REST Framework, we have different types of serializers to serialize object instances, and the serializers have different serializer relations to represent model relationships. In this section, we will discuss the different serializer relations provided by Django REST Framework Serializers.
Table of Contents
- Getting Started
- Creating Django Models and Views
- PrimaryKeyRelatedField
- StringRelatedField
- SlugRelatedField
- HyperlinkedIndetityField
- HyperlinkedRelatedField
- Nested Relationship