Skip to content

Commit

Permalink
Merge 8892613 into ed69c4f
Browse files Browse the repository at this point in the history
  • Loading branch information
sharrajesh committed Jun 3, 2018
2 parents ed69c4f + 8892613 commit 20a94a9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
25 changes: 20 additions & 5 deletions drf_queryfields/mixins.py
@@ -1,3 +1,5 @@
import six

class QueryFieldsMixin(object):

# If using Django filters in the API, these labels mustn't conflict with any model field names.
Expand All @@ -12,15 +14,17 @@ def __init__(self, *args, **kwargs):
super(QueryFieldsMixin, self).__init__(*args, **kwargs)

try:
request = self.context['request']
if not self.context:
return
request = self.context.get('request')
if not request:
return
method = request.method
if method != 'GET':
return
except (AttributeError, TypeError, KeyError):
# The serializer was not initialized with request context.
return

if method != 'GET':
return

try:
query_params = request.query_params
except AttributeError:
Expand All @@ -35,6 +39,7 @@ def __init__(self, *args, **kwargs):

if not include_field_names and not exclude_field_names:
# No user fields filtering was requested, we have nothing to do here.
self._reset_context_for_sub_fields(self.fields)
return

serializer_field_names = set(self.fields)
Expand All @@ -45,3 +50,13 @@ def __init__(self, *args, **kwargs):

for field in fields_to_drop:
self.fields.pop(field)

self._reset_context_for_sub_fields(self.fields)

def _reset_context_for_sub_fields(self, fields):
for field in six.itervalues(fields.fields):
if not field.context:
# print 'resetting context for {}'.format(type(field))
delattr(field, 'context')
setattr(field, '_context', self.context)
self._reset_context_for_sub_fields(field)
3 changes: 3 additions & 0 deletions setup.py
Expand Up @@ -24,6 +24,9 @@
license='MIT',
url='https://github.com/wimglenn/djangorestframework-queryfields',
classifiers=classifiers,
install_requires=[
'six',
],
extras_require={
'dev': [
'pytest', 'pytest-cov', 'pytest-django', 'coveralls',
Expand Down

0 comments on commit 20a94a9

Please sign in to comment.