Hi Richard,
If you want the choices to reflect the current values in the database when the filterset is instantiated then you will have to look them up every time the form is created.
This is a common problem with Django forms, and the usual solution is to have a form that gets the choices queryset when it is instantiated. You can write a custom form class that does this in its __init__, and then specify that as the form class to use in your FilterSet class:
class EventFilterForm(forms.Form):
def __init__(self, *args, **kwargs):
super(EventFilterForm, self).__init__(*args, **kwargs)
# Now get current tag choices.
tags = Event.objects.values_list('tag', flat=True).distinct()
tag_choices = [(tag, tag) for tag in tags]
self.fields['tag'].choices = tag_choices
class EventFilter(FilterSet):
tag = MultipleChoiceFilter()
class Meta:
model = Event
form = EventFilterForm
And Bob's your uncle!
You could also try using a ModelMultipleChoiceFilter with the tag queryset. This way you wouldn't need the custom form, but you would have to override the underlying model choice field somehow to let it use the string value to filter on the correct field (it would try filtering on the primary key by default).
Hope this helps, Dave