Use a slug field for ModelChoiceFilter #1344
-
I have a model which has a foreign key to an enum table. I'd rather filter by the enum name rather than the id. Product:
Quality:
I'd like to filter with something like My current solution is this:class ProductFilterSet(FilterSet):
class Meta:
model = Product
fields = ("quality",)
quality= CharFilter(field_name="quality", method="filter_quality")
def filter_quality(self, queryset, name, value):
return queryset.filter(**{f"{name}__name": value}) I want something like DRF's
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You should be able to use |
Beta Was this translation helpful? Give feedback.
You should be able to use
to_field_name
: https://django-filter.readthedocs.io/en/stable/ref/filters.html#to-field-name