Skip to content

Commit

Permalink
merged changes from master
Browse files Browse the repository at this point in the history
  • Loading branch information
seebass committed Mar 20, 2017
1 parent 4716d33 commit 17d691f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
25 changes: 15 additions & 10 deletions drf_nested_routing/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,34 @@
class NestedViewSetMixin(object):
def get_queryset(self):
queryset = super(NestedViewSetMixin, self).get_queryset()
self.__add_related_fetches_to_querySet(queryset)
return self.__filter_query_set_by_parent_lookups(queryset)
self._add_related_fetches_to_querySet(queryset)
return self._filter_query_set_by_parent_lookups(queryset)

def __add_related_fetches_to_querySet(self, queryset):
parent_lookups = drf_nested_routing.get_parent_query_lookups_by_class(queryset.model)
def _add_related_fetches_to_querySet(self, queryset):
related = getattr(self, 'select_related', [])
lookups = parent_lookups + related
lookups = related + self._get_parent_lookups_for_select_related(queryset)
if lookups:
queryset = queryset.select_related(*lookups)
prefetches = getattr(self, 'prefetch_related', [])
for prefetch in prefetches:
queryset = queryset.prefetch_related(prefetch)

def __filter_query_set_by_parent_lookups(self, queryset):
parents_query_dict = self.__get_parents_query_filter()
def _get_parent_lookups_for_select_related(self, queryset):
parent_lookups = drf_nested_routing.get_parent_query_lookups_by_class(queryset.model)
return parent_lookups

def _filter_query_set_by_parent_lookups(self, queryset):
parents_query_dict = self._get_parents_query_filter()
if parents_query_dict:
queryset = queryset.filter(**parents_query_dict)
return queryset

def __get_parents_query_filter(self):
def _get_parents_query_filter(self):
result = {}
for kwarg_name in self.kwargs:
if kwarg_name.startswith(drf_nested_routing.PARENT_LOOKUP_NAME_PREFIX):
query_lookup = kwarg_name.replace(drf_nested_routing.PARENT_LOOKUP_NAME_PREFIX, '', 1)
query_lookup = kwarg_name.replace(drf_nested_routing.PARENT_LOOKUP_NAME_PREFIX, '',
1)
query_value = self.kwargs.get(kwarg_name)
result[query_lookup] = query_value
# return parent query filter if not wildcard *
Expand All @@ -51,7 +55,8 @@ def _add_parent_to_request_data_through_lookup(self, request, **kwargs):
if '__' in parent_lookup_key_without_prefix:
continue # only the direct parent is added to data

self._add_parent_to_request_data(request, parent_lookup_key_without_prefix, kwargs[parent_lookup_key])
self._add_parent_to_request_data(request, parent_lookup_key_without_prefix,
kwargs[parent_lookup_key])


class CreateNestedModelMixin(AddParentToRequestDataMixin, CreateModelMixin):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setup(
name='drf-nested-routing',
version="0.9.4",
version="0.9.5",
url='https://github.com/seebass/drf-nested-routing',
license='MIT',
description='Nested routing extension for Django REST Framework 3',
Expand Down

0 comments on commit 17d691f

Please sign in to comment.