mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-02 20:54:42 +03:00
Add lookup_kwarg
This commit is contained in:
parent
099163f81f
commit
dc45bc7bfa
|
@ -26,6 +26,7 @@ class GenericAPIView(views.APIView):
|
||||||
pagination_serializer_class = api_settings.DEFAULT_PAGINATION_SERIALIZER_CLASS
|
pagination_serializer_class = api_settings.DEFAULT_PAGINATION_SERIALIZER_CLASS
|
||||||
allow_empty = True
|
allow_empty = True
|
||||||
page_kwarg = 'page'
|
page_kwarg = 'page'
|
||||||
|
lookup_kwarg = 'pk'
|
||||||
|
|
||||||
# Pending deprecation
|
# Pending deprecation
|
||||||
model = None
|
model = None
|
||||||
|
@ -167,23 +168,26 @@ class GenericAPIView(views.APIView):
|
||||||
By default this requires `self.queryset` and a `pk` or `slug` argument
|
By default this requires `self.queryset` and a `pk` or `slug` argument
|
||||||
in the URLconf, but subclasses can override this to return any object.
|
in the URLconf, but subclasses can override this to return any object.
|
||||||
"""
|
"""
|
||||||
# Use a custom queryset if provided; this is required for subclasses
|
# Determine the base queryset to use.
|
||||||
# like DateDetailView
|
|
||||||
if queryset is None:
|
if queryset is None:
|
||||||
queryset = self.get_queryset()
|
queryset = self.get_queryset()
|
||||||
# Next, try looking up by primary key.
|
|
||||||
|
# Perform the lookup filtering.
|
||||||
pk = self.kwargs.get(self.pk_url_kwarg, None)
|
pk = self.kwargs.get(self.pk_url_kwarg, None)
|
||||||
slug = self.kwargs.get(self.slug_url_kwarg, None)
|
slug = self.kwargs.get(self.slug_url_kwarg, None)
|
||||||
if pk is not None:
|
lookup = self.kwargs.get(self.lookup_kwarg, None)
|
||||||
|
|
||||||
|
if lookup is not None:
|
||||||
|
queryset = queryset.filter(**{self.lookup_kwarg: lookup})
|
||||||
|
elif pk is not None:
|
||||||
queryset = queryset.filter(pk=pk)
|
queryset = queryset.filter(pk=pk)
|
||||||
# Next, try looking up by slug.
|
|
||||||
elif slug is not None:
|
elif slug is not None:
|
||||||
queryset = queryset.filter(**{self.slug_field: slug})
|
queryset = queryset.filter(**{self.slug_field: slug})
|
||||||
# If none of those are defined, it's an error.
|
|
||||||
else:
|
else:
|
||||||
raise AttributeError("Generic detail view %s must be called with "
|
raise AttributeError("Generic detail view %s must be called with "
|
||||||
"either an object pk or a slug."
|
"either an object pk or a slug."
|
||||||
% self.__class__.__name__)
|
% self.__class__.__name__)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Get the single item from the filtered queryset
|
# Get the single item from the filtered queryset
|
||||||
obj = queryset.get()
|
obj = queryset.get()
|
||||||
|
@ -191,7 +195,9 @@ class GenericAPIView(views.APIView):
|
||||||
raise Http404(_("No %(verbose_name)s found matching the query") %
|
raise Http404(_("No %(verbose_name)s found matching the query") %
|
||||||
{'verbose_name': queryset.model._meta.verbose_name})
|
{'verbose_name': queryset.model._meta.verbose_name})
|
||||||
|
|
||||||
|
# May raise a permission denied
|
||||||
self.check_object_permissions(self.request, obj)
|
self.check_object_permissions(self.request, obj)
|
||||||
|
|
||||||
return obj
|
return obj
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -61,7 +61,7 @@ if django_filters:
|
||||||
class CommonFilteringTestCase(TestCase):
|
class CommonFilteringTestCase(TestCase):
|
||||||
def _serialize_object(self, obj):
|
def _serialize_object(self, obj):
|
||||||
return {'id': obj.id, 'text': obj.text, 'decimal': obj.decimal, 'date': obj.date}
|
return {'id': obj.id, 'text': obj.text, 'decimal': obj.decimal, 'date': obj.date}
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
"""
|
"""
|
||||||
Create 10 FilterableItem instances.
|
Create 10 FilterableItem instances.
|
||||||
|
@ -190,7 +190,7 @@ class IntegrationTestDetailFiltering(CommonFilteringTestCase):
|
||||||
Integration tests for filtered detail views.
|
Integration tests for filtered detail views.
|
||||||
"""
|
"""
|
||||||
urls = 'rest_framework.tests.filterset'
|
urls = 'rest_framework.tests.filterset'
|
||||||
|
|
||||||
def _get_url(self, item):
|
def _get_url(self, item):
|
||||||
return reverse('detail-view', kwargs=dict(pk=item.pk))
|
return reverse('detail-view', kwargs=dict(pk=item.pk))
|
||||||
|
|
||||||
|
@ -221,7 +221,7 @@ class IntegrationTestDetailFiltering(CommonFilteringTestCase):
|
||||||
response = self.client.get('{url}?decimal={param}'.format(url=self._get_url(low_item), param=search_decimal))
|
response = self.client.get('{url}?decimal={param}'.format(url=self._get_url(low_item), param=search_decimal))
|
||||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
self.assertEqual(response.data, low_item_data)
|
self.assertEqual(response.data, low_item_data)
|
||||||
|
|
||||||
# Tests that multiple filters works.
|
# Tests that multiple filters works.
|
||||||
search_decimal = Decimal('5.25')
|
search_decimal = Decimal('5.25')
|
||||||
search_date = datetime.date(2012, 10, 2)
|
search_date = datetime.date(2012, 10, 2)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user