Permalink
Browse files

Fixed an issue where queryset needed to be defined for build_filters …

…to succeed on a ModelResource. Affected alternate implementations of ModelResource.

Thanks Mitar for the patch, test, and follow-up
Closes #670
  • Loading branch information...
1 parent 9819db5 commit 342076cf6bfacd1b8734e02e6ffe225e590114c4 @issackelly issackelly committed Oct 20, 2012
Showing with 22 additions and 3 deletions.
  1. +1 −1 AUTHORS
  2. +7 −2 tastypie/resources.py
  3. +14 −0 tests/core/tests/resources.py
View
@@ -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.
View
@@ -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.
@@ -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)
@@ -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'
@@ -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()

0 comments on commit 342076c

Please sign in to comment.