You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is there a way to customize what filters are shown on the view level?
For example, i would like to show a filter only if there are more than 1 of Foo in the database.
Something like this
class IncomingInvoices(SingleTableMixin, FilterView):
model = IncomingInvoice
filterset_class = IncomingInvoiceFilter
def get_filterset_kwargs(self, filterset_class):
kwargs = super().get_filterset_kwargs(filterset_class)
businesses = Business.objects.all()
if businesses.count() > 1:
kwargs["fields"].append("business")
return kwargs
But i cant seem to do this, i have tried lots of ways, but the problem is that the Meta class in filterset is not updateable at all it seems
The only dirty way i got it to work is like this:
class IncomingInvoiceFilter(django_filters.FilterSet):
book_date = DateFromToRangeFilter(widget=RangeWidget(attrs={"class": "datepicker", "type": "date"}))
class Meta:
model = IncomingInvoice
fields = ["book_date", "company"]
if "makemigrations" not in sys.argv and "migrate" not in sys.argv and "manage.py" not in sys.argv:
businesses = Business.objects.all()
if businesses.count() > 1:
fields.append("business")
This discussion was converted from issue #1482 on February 21, 2022 13:06.
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi there,
Is there a way to customize what filters are shown on the view level?
For example, i would like to show a filter only if there are more than 1 of Foo in the database.
Something like this
But i cant seem to do this, i have tried lots of ways, but the problem is that the
Meta
class in filterset is not updateable at all it seemsThe only dirty way i got it to work is like this:
Beta Was this translation helpful? Give feedback.
All reactions