Skip to content

Commit 7667278

Browse files
committed
Added . Closes #1188.
1 parent c37bd40 commit 7667278

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

docs/api-guide/generic-views.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ The following attributes control the basic view behavior.
6565

6666
* `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.
6767
* `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.
68-
* `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.
68+
* `lookup_field` - The model field that should be used to for performing object lookup of individual model instances. Defaults to `'pk'`. Note that when using hyperlinked APIs you'll need to ensure that *both* the API views *and* the serializer classes set the lookup fields if you need to use a custom value.
69+
* `lookup_url_kwarg` - The URL keyword argument that should be used for object lookup. The URL conf should include a keyword argument corresponding to this value. If unset this defaults to using the same value as `lookup_field`.
6970

7071
**Shortcuts**:
7172

rest_framework/generics.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class GenericAPIView(views.APIView):
5454
# If you want to use object lookups other than pk, set this attribute.
5555
# For more complex lookup requirements override `get_object()`.
5656
lookup_field = 'pk'
57+
lookup_url_kwarg = None
5758

5859
# Pagination settings
5960
paginate_by = api_settings.PAGINATE_BY
@@ -278,9 +279,11 @@ def get_object(self, queryset=None):
278279
pass # Deprecation warning
279280

280281
# Perform the lookup filtering.
282+
# Note that `pk` and `slug` are deprecated styles of lookup filtering.
283+
lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field
284+
lookup = self.kwargs.get(lookup_url_kwarg, None)
281285
pk = self.kwargs.get(self.pk_url_kwarg, None)
282286
slug = self.kwargs.get(self.slug_url_kwarg, None)
283-
lookup = self.kwargs.get(self.lookup_field, None)
284287

285288
if lookup is not None:
286289
filter_kwargs = {self.lookup_field: lookup}

0 commit comments

Comments
 (0)