Dynamic Filter Choice Field In Django
I am using django-filter to filter results on a page. I am able to use static choice filters like this: filters.py FILTER_CHOICES = ( ('', 'All States'), ('AL', 'Alabama'), ('AK',
Solution 1:
That's because FILTER_CHOICES
is tuples in a tuple, not tuples in a list. Try this:
plant_number = django_filters.ChoiceFilter(choices=((o.plant_number.id, o.plant_number.foobar + " " + o.manufacturer_name) for o in Plant.objects.all()))
Solution 2:
So I figured this out, I was passing the plant_number instead of the id, which is what django was expecting since my filter is against the Sightings model and plant_number is a foreign key in the the Sightings model. The code that worked:
plant_number = django_filters.ChoiceFilter(choices=[[o.id, o.plant_number + " " + o.Manufacturer] for o in Plant.objects.all().order_by('IMS_plant')])
Post a Comment for "Dynamic Filter Choice Field In Django"