HyperlinkedModelSerializer
The HyperlinkedModelSerializer class is similar to the ModelSerializer class except that it uses hyperlinks to represent relationships, rather than primary keys. By default the serializer will include a url field instead of a primary key field. The url field will be represented using a HyperlinkedIdentityField serializer field, and any relationships on the model will be represented using a HyperlinkedRelatedField serializer field.
Syntax :
Python3
class SerializerName(serializers.HyperlinkedModelSerializer): class Meta: model = ModelName fields = List of Fields |
Example :
Python3
class AccountSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = Account fields = [ 'id' , 'account_name' , 'users' , 'created' ] |
To checkout how to use HyperLinkedModelSerializer in your project, visit – HyperlinkedModelSerializer in serializers – Django REST Framework.
Serializers – Django REST Framework
The serializers in the REST framework work very similarly to Django’s Form and ModelForm classes. The two major serializers that are most popularly used are ModelSerializer and HyperLinkedModelSerialzer. This article revolves around how to use serializers from scratch in Django REST Framework to advanced serializer fields and arguments. It assumes one is familiar with How to start a project with Django REST Framework.
- Creating and Using Serializers
- ModelSerializer
- HyperLinkedModelSerializer
- Serializer Fields
- Core arguments in serializer fields