Skip to content

Commit

Permalink
Use QueryDict for default data.
Browse files Browse the repository at this point in the history
  • Loading branch information
carltongibson committed Dec 28, 2024
1 parent 25fb436 commit cd41c81
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions django_filters/filterset.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django.db import models
from django.db.models.constants import LOOKUP_SEP
from django.db.models.fields.related import ManyToManyRel, ManyToOneRel, OneToOneRel
from django.utils.datastructures import MultiValueDict
from django.http import QueryDict

from .conf import settings
from .constants import ALL_FIELDS
Expand Down Expand Up @@ -192,7 +192,7 @@ def __init__(self, data=None, queryset=None, *, request=None, prefix=None):
model = queryset.model

self.is_bound = data is not None
self.data = data or MultiValueDict()
self.data = data or QueryDict()
self.queryset = queryset
self.request = request
self.form_prefix = prefix
Expand Down
12 changes: 6 additions & 6 deletions django_filters/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
from django import forms
from django.db.models.fields import BLANK_CHOICE_DASH
from django.forms.utils import flatatt
from django.http import QueryDict
from django.utils.datastructures import MultiValueDict
from django.utils.encoding import force_str
from django.utils.http import urlencode
from django.utils.safestring import mark_safe
from django.utils.translation import gettext as _

Expand Down Expand Up @@ -56,13 +56,13 @@ def render_option(self, name, selected_choices, option_value, option_label):
option_value = force_str(option_value)
if option_label == BLANK_CHOICE_DASH[0][1]:
option_label = _("All")

data = QueryDict(mutable=True)
data.update(self.data)
data = self.data.copy()
data[name] = option_value

selected = data == self.data or option_value in selected_choices
url = data.urlencode()
try:
url = data.urlencode()
except AttributeError:
url = urlencode(data)
return self.option_string() % {
"attrs": selected and ' class="selected"' or "",
"query_string": url,
Expand Down
6 changes: 3 additions & 3 deletions tests/test_widgets.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.forms import NumberInput, Select, TextInput
from django.test import TestCase
from django.utils.datastructures import MultiValueDict
from django.http import QueryDict
from django_filters.widgets import (
BaseCSVWidget,
BooleanWidget,
Expand Down Expand Up @@ -143,15 +143,15 @@ def test_widget_with_blank_choice(self):
</ul>""",
)

def test_widget_multivaluedict(self):
def test_widget_with_empty_querydict(self):
choices = (
("", "---------"),
("test-val1", "test-label1"),
("test-val2", "test-label2"),
)

w = LinkWidget(choices=choices)
w.value_from_datadict(MultiValueDict(), {}, "price")
w.value_from_datadict(QueryDict(), {}, "price")
self.assertHTMLEqual(
w.render("price", ""),
"""
Expand Down

0 comments on commit cd41c81

Please sign in to comment.