Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing support for subresources and non-Django backends #670

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions tastypie/resources.py
Expand Up @@ -1758,7 +1758,7 @@ def build_filters(self, filters=None):

qs_filters = {}

if hasattr(self._meta, 'queryset'):
if getattr(self._meta, 'queryset', None) is not None:
# Get the possible query terms from the current QuerySet.
if hasattr(self._meta.queryset.query.query_terms, 'keys'):
# Django 1.4 & below compatibility.
Expand All @@ -1767,7 +1767,12 @@ def build_filters(self, filters=None):
# Django 1.5+.
query_terms = self._meta.queryset.query.query_terms
else:
query_terms = QUERY_TERMS.keys()
if hasattr(QUERY_TERMS, 'keys'):
# Django 1.4 & below compatibility.
query_terms = QUERY_TERMS.keys()
else:
# Django 1.5+.
query_terms = QUERY_TERMS

for filter_expr, value in filters.items():
filter_bits = filter_expr.split(LOOKUP_SEP)
Expand Down
14 changes: 14 additions & 0 deletions tests/core/tests/resources.py
Expand Up @@ -703,6 +703,16 @@ def get_resource_uri(self, bundle_or_obj=None, url_name='api_dispatch_list'):
return '/api/v1/notes/%s/' % bundle_or_obj.obj.id


class NoQuerysetNoteResource(ModelResource):
class Meta:
resource_name = 'noqsnotes'
authorization = Authorization()
filtering = {
'name': ALL,
}
object_class = Note


class LightlyCustomNoteResource(NoteResource):
class Meta:
resource_name = 'noteish'
Expand Down Expand Up @@ -1521,6 +1531,10 @@ def test_build_filters(self):
# Make sure that fields that don't have attributes can't be filtered on.
self.assertRaises(InvalidFilterError, resource_4.build_filters, filters={'notes__hello_world': 'News'})

# Make sure build_filters works even on resources without queryset
resource = NoQuerysetNoteResource()
self.assertEqual(resource.build_filters(), {})

def test_apply_sorting(self):
resource = NoteResource()

Expand Down