Make searchable checkboxes checked by default
Solution courtesy of Jason (thanks!)
@@ -30,13 +30,24 @@ class PartnerFilter(django_filters.FilterSet):
widget=forms.CheckboxSelectMultiple,
)
- def __init__(self, *args, **kwargs):
+ def __init__(self, data=None, *args, **kwargs):
# grab "language_code" from kwargs and then remove it so we can call super()
language_code = None
if "language_code" in kwargs:
language_code = kwargs.get("language_code")
kwargs.pop("language_code")
- super(PartnerFilter, self).__init__(*args, **kwargs)
+
+ # Set searchable to all selected by default
+ # if filterset is bound, use initial values as defaults
+ if data is not None:
+ # get a mutable copy of the QueryDict
+ data = data.copy()
+ for name, f in self.base_filters.items():
+ # filter param is either missing or empty, set all searchable values in MultiValueDict
+ if name == "searchable" and not data.get(name):
+ data.setlist("searchable", ["0", "1", "2"])
+
+ super(PartnerFilter, self).__init__(data, *args, **kwargs)
self.filters["tags"].extra.update({"choices": get_tag_choices(language_code)})
# Add CSS classes to style widgets
self.filters["tags"].field.widget.attrs.update(
