python - Filtering by foreign key in dropdown -
i'm using django-filters.
my auto model has manufacturer foreign key . want filter cars dropdown populated manufacturers in database.
class car(models.model): slug = models.slugfield(unique=true) name = models.charfield(max_length=256) cost = models.decimalfield(max_digits=10,decimal_places=5) manufacturer = models.foreignkey(manufacturer)
my filter blank text field, can come in manufacturer name , submit filter way. dropdown much more suitable, haven't been able find way this. here filter model now:
class carfilter(django_filters.filterset): manufacturer = django_filters.charfilter(name="manufacturer__name") class meta: model = auto fields = ['name', 'manufacturer']
just don't define manufacturer filter field at all , django-filter utilize default filter field (which drop down). should working:
class carfilter(django_filters.filterset): class meta: model = auto fields = ['name', 'manufacturer'] python django filter django-filter
No comments:
Post a Comment