I am using django-filter and need to add a ChoiceFilter
with choices dependent on the request that I receive. I am reading the docs for ChoiceFilter but it says: This filter matches values in its choices argument. The choices must be explicitly passed when the filter is declared on the FilterSet
.
So is there any way to get request-dependent choices in the ChoiceFilter
?
I haven't actually written the code but the following is what I want -
class F(FilterSet):
status = ChoiceFilter(choices=?) #choices depend on request
class Meta:
model = User
fields = ['status']
request
(which may beNone
so you should check), so you can userequest
object in the filter's__init__()
method and declare thestatus
field there. – Rebecarequest
property. – Rebecaself.fields['status'] = ChoiceFilter(choices=...)
in__init__
but was getting some error. I'm currently out so cannot check but iirc the problem was withself.fields['status']
being set like this. – Consistency'F' object has no attribute 'fields'
– Consistency