Skip to content

Instantly share code, notes, and snippets.

@kezabelle
Created March 29, 2017 14:49
Show Gist options
  • Save kezabelle/d6ae5e69366f56e6411633ad69050643 to your computer and use it in GitHub Desktop.
Save kezabelle/d6ae5e69366f56e6411633ad69050643 to your computer and use it in GitHub Desktop.
django-filters .. paring down the choices in a related widget.
from django_filters import FilterSet
class MyModelFilterSet(FilterSet):
class Meta:
model = MyModel
fields = ['my_relation'] # internally this is a reverse relation, so MyModelInstance.my_relation_set.all()
# How do I get it so that the ModelChoiceFilter (actually a ModelMultipleChoiceFilter!) is only
# those items which exist given the FilterSet's original queryset (self.queryset.all())
@kezabelle
Copy link
Author

Ended up doing it in the filterset's init method. Bit nasty, but it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment