#Multiple form controls for one field in django-filter

4 messages · Page 1 of 1 (latest)

somber sundial
#

So I have some okayish hacky ways to do get this working but roughly I have one field, a DateRangeField, but want to have the user choose two things, a year (from a dropdown) and a quater (as radio buttons).

So for the user the querystring would end up as ?year=2024&quater=1.

But struggling to find a way to get django-filter to do it. I have a feeling I need to override the form and use a MultiWidget, but not sure how that would affect the querystring or work with django-filter. Anyone done something like this?

somber sundial
#

I found a way! It's just slightly annoying. Needed to use a combination of InFilter and SuffixMultiWidget

restive trellis
#

Not to hijack your thread, but out of curiosity, do you "generate" the django-filter form components from the lib itself, or do you manually build them?

somber sundial
#

personally I just do {{ filter.form }} and then style the form in CSS. If I need to do anything custom (quite often with filtering forms) I will specify the form for the filter and in the form class specify the form template to use, so in my template it's always just {{ filter.form }} and I don't have to gunk up the main template too much