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?
2 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 HyperlinkedIdentityField
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
, in the 2nd:
- 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
.

- 334
- 4
- 13

- 40,873
- 40
- 203
- 237
-
2For a `url` field, I believe you mean a `HyperlinkedIdentityField`? And in your description of the two differences, the bullets apply to `HyperlinkedIdentityField`, correct? – mcastle Jul 25 '15 at 09:47
-
`"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. "` I think the answerer means `HyperlinkedIdentityField` there instead of `HyperlinkedRelatedField`. – theoctober19th Jun 05 '21 at 13:31
The obvious answer is that HyperLinkedIdentityField
is meant to point to the current object only, whereas HyperLinkedRelatedField
is meant to point to something that the current object references. I suspect under the hood the two are different only in that the identity field has less work to do in order to find the related model's URL routes (because the related model is the current model), while the related field has to actually figure out the right URLs for some other model.
In other words, HyperLinkedIdentityField
is lighter-weight (more efficient), but won't work for models other than the current model.

- 2,147
- 3
- 20
- 37
-
i try to add data after I truncate the database it works but after much data I got error with HyperLinkedIdentityField like ``` Could not resolve URL for hyperlinked relationship using view name "job-detail". You may have failed to include the related model in your API, or incorrectly configured the `lookup_field` attribute on this field. ``` – perymerdeka May 08 '21 at 16:42