HyperlinkedIndetityField
For Hyperlinking the API, the serializer class should extend HyperlinkedModelSerializer. The HyperlinkedModelSerializer class makes use of hyperlinks to represent relationships, instead of primary keys. By default, the serializer class that is inherited from HyperlinkedModelSerializer class will include a url field instead of a primary key field. The url field will be represented using a HyperlinkedIdentityField serializer field. It can also be used for an attribute on the object.
The HyperlinkedIdentityField is always read-only. The arguments of HyperlinkedIdentitField are:
- view_name – The view name is used as the target of the relationship. In standard router classes, this will be a string with the format <model_name>-detail.
- lookup_field – The field on the target, which is used for the lookup. It corresponds to a URL keyword argument on the referenced view. Default is ‘pk’.
- lookup_url_kwarg – The name of the keyword argument defined in the URL conf that corresponds to the lookup field. Defaults to using the same value as lookup_field.
- format – If using format suffixes, hyperlinked fields will use the same format suffix for the target unless overridden by using the format argument.
In the EmployeeTaskSerializer class, you can use represent the url field as shown below:
url = serializers.HyperlinkedIdentityField( view_name='employeetask-detail', lookup_field='pk' )
Note: By default, the serializer class that is inherited from HyperlinkedModelSerializer class will include a url field, and the view name will be a string with the format <model_name>-detail.
The EmployeeTaskSerializer class as follows:
Python3
class EmployeeTaskSerializer(serializers.HyperlinkedModelSerializer): url = serializers.HyperlinkedIdentityField( view_name = 'employeetask-detail' , lookup_field = 'pk' ) employee = serializers.SlugRelatedField( queryset = Employee.objects. all (), slug_field = 'name' ) class Meta: model = EmployeeTask fields = '__all__' |
When instantiating a HyperlinkedModelSerializer, it is a must to include the current request in the serializer context. You need to pass context={‘request’:request} as an argument when instantiating your EmployeeTaskSerializer. For example:
emp_serializer = EmployeeSerializer(emp, many=True, context={'request':request})
Make sure you edit your employee_list and employee_detail function in the views.py file with the above code. You should add context={‘request’:request} argument while instantiating EmployeeTaskSerializer (GET, POST, PUT and PATCH).
Note: Note: This context along with the lookup_field is used for generating a fully qualified url by HyperlinkedIdentityField
Let’s retrieve the EmployeeTask values to understand how the values are displayed. The HTTPie command is
http GET :8000/task/1/
Output
HTTP/1.1 200 OK Content-Length: 296 Content-Type: application/json Date: Fri, 22 Jan 2021 16:16:42 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": "Mathew A", "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
If we analyze the output, we can notice that instead of the primary key it displays url field.
"url": "http://localhost:8000/task/1/"
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
Contact Us