Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For Django Rest Framework, what is the difference in use case for HyperLinkedRelatedField and HyperLinkedIdentityField?

I've of course reviewed the docs, but was wondering if anyone could more succinctly explain the difference in use case and application between these fields. Why would one use one field over the other? Would there be a difference between these fields for a OneToOne relationship?

like image 203
mcastle Avatar asked Jul 22 '15 14:07

mcastle


People also ask

What is PrimaryKeyRelatedField in Django?

PrimaryKeyRelatedField. PrimaryKeyRelatedField may be used to represent the target of the relationship using its primary key. For example, the following serializer: class AlbumSerializer(serializers. ModelSerializer): tracks = serializers.

What is the difference between ModelSerializer and 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.

How do I change the foreign key value in Django REST Framework?

As stated in the documentation, you will need to write your own create() and update() methods in your serializer to support writable nested data. You will also need to explicitly add the status field instead of using the depth argument otherwise I believe it won't be automatically added to validated_data .

What is difference between Django and Django REST Framework?

Django is the web development framework in python whereas the Django Rest Framework is the library used in Django to build Rest APIs. Django Rest Framework is especially designed to make the CRUD operations easier to design in Django. Django Rest Framework makes it easy to use your Django Server as an REST API.


1 Answers

You would use a HyperlinkedIdentityField to link to the object currently being serialized and a HyperlinkedRelatedField to link to objects related to the one being serialized.

So for a one-to-one relationship, foreign key relationship, many-to-many relationship and basically anything else involving relationships (in Django models), you want to use a HyperlinkedRelatedField. The only time where a HyperlinkedRelatedField is used is for the url field which you can include on your serializer to point to the current object.


In Django REST framework 3.0.0, there are only two differences between a HyperlinkedRelatedField and HyperlinkedIdentityField.

  • The source is automatically set to * (the current object)
  • It is set to read_only=True, so it can't be changed

Which means that setting a HyperlinkedRelatedField with those properties is exactly the same as having a HyperlinkedIdentityField.


In older versions of Django REST framework (before 3.0.0), the HyperlinkedIdentityField used to be a dedicated field for resolving the url for the current object. It accepted a slightly different set of parameters and was not a subclass of HyperlinkedRelatedField.

like image 134
Kevin Brown-Silva Avatar answered Oct 11 '22 05:10

Kevin Brown-Silva