Skip to content

0.9.1 backports #141

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

Merged
merged 3 commits into from
Nov 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Unreleased:
-----------

v0.9.1:
-------

* #128 Fix all lookups handling for related fields
* #129 Fix backend template rendering

v0.9.0:
-------

Expand Down
13 changes: 8 additions & 5 deletions rest_framework_filters/backends.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

from django.template import loader
from django.template import Template, TemplateDoesNotExist, loader
from rest_framework import compat
from django_filters.rest_framework import backends

Expand Down Expand Up @@ -28,8 +28,11 @@ def to_html(self, request, queryset, view):
# forces `form` evaluation before `qs` is called. This prevents an empty form from being cached.
filter_instance.form

context = {
try:
template = loader.get_template(self.template)
except TemplateDoesNotExist:
template = Template(backends.template_default)

return compat.template_render(template, context={
'filter': filter_instance
}
template = loader.get_template(self.template)
return compat.template_render(template, context)
})
3 changes: 2 additions & 1 deletion rest_framework_filters/filterset.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from django.utils import six

from django_filters import filterset, rest_framework
from django_filters.utils import get_model_field

from . import filters
from . import utils
Expand Down Expand Up @@ -139,7 +140,7 @@ def filters_for_model(cls, model, opts):
lookups = '__all__'

if lookups == '__all__':
field = model._meta.get_field(name)
field = get_model_field(model, name)
fields[name] = utils.lookups_for_field(field)

return filterset.filters_for_model(
Expand Down
28 changes: 27 additions & 1 deletion tests/test_backends.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@

from rest_framework.test import APITestCase
from rest_framework.test import APITestCase, APIRequestFactory

from .testapp import models, views

factory = APIRequestFactory()


class BackendTest(APITestCase):

Expand Down Expand Up @@ -33,3 +35,27 @@ def test_filter_fields_reusability(self):
self.assertEqual(len(response.data), 1)
self.assertEqual(response.data[0]['username'], 'user1')
self.assertDictEqual(views.FilterFieldsUserViewSet.filter_fields, {'username': '__all__'})

def test_backend_output_sanity(self):
"""
Sanity check to ensure backend can at least render something without crashing.
"""
class SimpleViewSet(views.FilterFieldsUserViewSet):
filter_fields = ['username']

view = SimpleViewSet(action_map={})
backend = view.filter_backends[0]
request = view.initialize_request(factory.get('/'))
html = backend().to_html(request, view.get_queryset(), view)

self.assertHTMLEqual(html, """
<h2>Field filters</h2>
<form class="form" action="" method="get">
<p>
<label for="id_username">Username:</label>
<input id="id_username" name="username" type="text" />
<span class="helptext">Filter</span>
</p>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
""")
11 changes: 11 additions & 0 deletions tests/test_filterset.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ class Meta:
self.assertIsInstance(F.base_filters['author'], filters.ModelChoiceFilter)
self.assertIsInstance(F.base_filters['author__in'], BaseInFilter)

def test_alllookupsfilter_for_related_field(self):
# See: https://github.com/philipn/django-rest-framework-filters/issues/127
class F(FilterSet):
author = filters.AllLookupsFilter(name='author__last_name')

class Meta:
model = Note

self.assertIsInstance(F.base_filters['author'], filters.CharFilter)
self.assertEqual(F.base_filters['author'].name, 'author__last_name')

def test_relatedfilter_combined_with__all__(self):
# ensure that related filter is compatible with __all__ lookups.
class F(FilterSet):
Expand Down