You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/api-guide/serializers.md
+13-25Lines changed: 13 additions & 25 deletions
Original file line number
Diff line number
Diff line change
@@ -567,21 +567,22 @@ There needs to be a way of determining which views should be used for hyperlinki
567
567
568
568
By default hyperlinks are expected to correspond to a view name that matches the style `'{model_name}-detail'`, and looks up the instance by a `pk` keyword argument.
569
569
570
-
You can change the field that is used for object lookups by setting the `lookup_field` option. The value of this option should correspond both with a kwarg in the URL conf, and with a field on the model. For example:
570
+
You can override a URL field view name and lookup field by using either, or both of, the `view_name` and `lookup_field` options in the `extra_field_kwargs` setting, like so:
571
571
572
572
class AccountSerializer(serializers.HyperlinkedModelSerializer):
Note that the `lookup_field` will be used as the default on *all* hyperlinked fields, including both the URL identity, and any hyperlinked relationships.
For more specific requirements such as specifying a different lookup for each field, you'll want to set the fields on the serializer explicitly. For example:
581
+
Alternatively you can set the fields on the serializer explicitly. For example:
581
582
582
583
class AccountSerializer(serializers.HyperlinkedModelSerializer):
583
584
url = serializers.HyperlinkedIdentityField(
584
-
view_name='account-detail',
585
+
view_name='accounts',
585
586
lookup_field='slug'
586
587
)
587
588
users = serializers.HyperlinkedRelatedField(
@@ -595,28 +596,15 @@ For more specific requirements such as specifying a different lookup for each fi
**Tip**: Properly matching together hyperlinked representations and your URL conf can sometimes be a bit fiddly. Printing the `repr` of a `HyperlinkedModelSerializer` instance is a particularly useful way to inspect exactly which view names and lookup fields the relationships are expected to map too.
609
602
610
-
**Note**: The generic view implementations normally generate a `Location` header in response to successful `POST` requests. Serializers using `url_field_name` option will not have this header automatically included by the view. If you need to do so you will ned to also override the view's `get_success_headers()` method.
603
+
---
611
604
612
-
You can also override the URL field's view name and lookup field without overriding the field explicitly, by using the `view_name` and `lookup_field` options, like so:
605
+
## Changing the URL fieldname
613
606
614
-
class AccountSerializer(serializers.HyperlinkedModelSerializer):
0 commit comments