SlugRelatedField
The SlugRelatedField represents the target of the relationship using a field on the target. By default, the field permits read-write operation. For write operations, the slug field that represents a model field should be unique. The SlugRelatedField has the following arguments:
- slug_field – A field on the target and it should uniquely identify any given instance.
- queryset – It facilitates model instance lookups when validating the field input. Relationships must either set a queryset explicitly, or set read_only=True.
- many – Set this argument to True to serialize more than one relationship
- allow_null – If set to True, the field will accept values of None or the empty string for nullable relationships. Defaults to False.
In the EmployeeTaskSerializer class, you can replace the employee field representation with the below code:
employee = serializers.SlugRelatedField( queryset=Employee.objects.all(), slug_field='name')
The EmployeeTaskSerializer code as follows:
Python3
class EmployeeTaskSerializer(serializers.ModelSerializer): # SlugRelatedField employee = serializers.SlugRelatedField( queryset = Employee.objects. all (), slug_field = 'name' ) class Meta: model = EmployeeTask fields = ( 'pk' , 'task_name' , 'employee' , 'task_desc' , 'created_date' , 'deadline' ) |
The HTTPie command to create a new employee task is
http :8000/task/ task_name=”SlugRelatedField” employee=”Mathew A” task_desc=”Serialize relationship using SlugRelateField” deadline=”2021-01-27 00:00:00.000000+00:00″
Output
HTTP/1.1 201 Created Content-Length: 210 Content-Type: application/json Date: Fri, 22 Jan 2021 04:41:12 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-22T04:41:12.424001Z", "deadline": "2021-01-27T00:00:00Z", "employee": "Mathew A", "pk": 7, "task_desc": "Serialize relationship using SlugRelateField", "task_name": "SlugRelatedField" }
Sharing the command prompt screenshot for your reference
Here, while creating a new task, we have given the employee name in the employee field rather than the primary key. You can also mention employee id while representing SlugRelatedField. You should make sure that it satisfies the unique constraint.
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