Skip to content

Commit

Permalink
Add SuffixedMultiWidget support for django < 1.11
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan P Kilby committed Apr 8, 2017
1 parent 9445bda commit b134d36
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions django_filters/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from collections import Iterable
from itertools import chain
from re import search, sub
try:
from urllib.parse import urlencode
except:
Expand Down Expand Up @@ -123,6 +124,22 @@ def value_omitted_from_data(self, data, files, name):
for widget, suffix in zip(self.widgets, self.suffixes)
)

# Django < 1.11 compat
def format_output(self, rendered_widgets):
rendered_widgets = [
self.replace_name(output, i)
for i, output in enumerate(rendered_widgets)
]
return '\n'.join(rendered_widgets)

def replace_name(self, output, index):
result = search(r'name="(?P<name>.*)_%d"' % index, output)
name = result.group('name')
name = self.suffixed(name, self.suffixes[index])
name = 'name="%s"' % name

return sub(r'name=".*_%d"' % index, name, output)


class RangeWidget(SuffixedMultiWidget):
template_name = 'django_filters/widgets/multiwidget.html'
Expand All @@ -134,6 +151,10 @@ def __init__(self, attrs=None):

def format_output(self, rendered_widgets):
# Method was removed in Django 1.11.
rendered_widgets = [
self.replace_name(output, i)
for i, output in enumerate(rendered_widgets)
]
return '-'.join(rendered_widgets)

def decompress(self, value):
Expand Down

0 comments on commit b134d36

Please sign in to comment.