Skip to content

Commit 5d0aeef

Browse files
committed
Better docs related to lookup_field and hyperlinked serializers. Closes #920.
1 parent 351e172 commit 5d0aeef

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

docs/api-guide/generic-views.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ The following attributes control the basic view behavior.
6060

6161
* `queryset` - The queryset that should be used for returning objects from this view. Typically, you must either set this attribute, or override the `get_queryset()` method.
6262
* `serializer_class` - The serializer class that should be used for validating and deserializing input, and for serializing output. Typically, you must either set this attribute, or override the `get_serializer_class()` method.
63-
* `lookup_field` - The field that should be used to lookup individual model instances. Defaults to `'pk'`. The URL conf should include a keyword argument corresponding to this value. More complex lookup styles can be supported by overriding the `get_object()` method.
63+
* `lookup_field` - The field that should be used to lookup individual model instances. Defaults to `'pk'`. The URL conf should include a keyword argument corresponding to this value. More complex lookup styles can be supported by overriding the `get_object()` method. Note that when using hyperlinked APIs you'll need to ensure that *both* the API views *and* the serializer classes use lookup fields that correctly correspond with the URL conf.
6464

6565
**Shortcuts**:
6666

docs/api-guide/serializers.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,14 +387,16 @@ There needs to be a way of determining which views should be used for hyperlinki
387387

388388
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.
389389

390-
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 an field on the model. For example:
390+
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:
391391

392392
class AccountSerializer(serializers.HyperlinkedModelSerializer):
393393
class Meta:
394394
model = Account
395395
fields = ('url', 'account_name', 'users', 'created')
396396
lookup_field = 'slug'
397397

398+
Not that the `lookup_field` will be used as the default on *all* hyperlinked fields, including both the URL identity, and any hyperlinked relationships.
399+
398400
For more specfic requirements such as specifying a different lookup for each field, you'll want to set the fields on the serializer explicitly. For example:
399401

400402
class AccountSerializer(serializers.HyperlinkedModelSerializer):

0 commit comments

Comments
 (0)