Skip to content

Commit

Permalink
Fixed an issue where queryset needed to be defined for build_filters …
Browse files Browse the repository at this point in the history
…to succeed on a ModelResource. Affected alternate implementations of ModelResource.

Thanks Mitar for the patch, test, and follow-up
Closes #670
  • Loading branch information
issackelly committed Oct 20, 2012
1 parent 9819db5 commit 342076c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion AUTHORS
Expand Up @@ -65,7 +65,7 @@ Contributors:
* Vladimir Volodin (vvolodin) for patching ToManyField callable attributes.
* Mike Urbanski (mcu) for patching ``Paginator.get_limit``.
* Jason Brownbridge (jbrownbridge) for patching multiple ``offset/limit`` params appearing in pagination URIs.

* Mitar for compatability patches with django-tastypie-mongoengine

Thanks to Tav for providing validate_jsonp.py, placed in public domain.

Expand Down
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

0 comments on commit 342076c

Please sign in to comment.