only check for pk and slug when relevant

This commit is contained in:
Thomas Steen Rasmussen 2018-07-17 22:47:14 +02:00
parent 32e9b7c40b
commit 340a3eb58c
1 changed files with 10 additions and 8 deletions

View File

@ -28,16 +28,18 @@ class CampViewMixin(object):
filter_dict = {_filter: self.camp}
# get pk from kwargs if we have it
pk = self.kwargs.get(self.pk_url_kwarg)
if pk is not None:
# We should also filter for the pk of the object
filter_dict['pk'] = pk
if hasattr(self, 'pk_url_kwarg'):
pk = self.kwargs.get(self.pk_url_kwarg)
if pk is not None:
# We should also filter for the pk of the object
filter_dict['pk'] = pk
# get slug from kwargs if we have it
slug = self.kwargs.get(self.slug_url_kwarg)
if slug is not None and (pk is None or self.query_pk_and_slug):
# we should also filter for the slug of the object
filter_dict[self.get_slug_field()] = slug
if hasattr(self, 'slug_url_kwarg'):
slug = self.kwargs.get(self.slug_url_kwarg)
if slug is not None and (pk is None or self.query_pk_and_slug):
# we should also filter for the slug of the object
filter_dict[self.get_slug_field()] = slug
# do the filtering and return the result
result = queryset.filter(**filter_dict)