1👍
Use ChoiceFilter field instead of ModelChoiceFilter
class PeriodFilter(django_filters.rest_framework.FilterSet):
type = django_filters.ChoiceFilter(choices=Period.PeriodType.choices, field_name='period__type', label='Type')
0👍
After reading the docs it seems ModelChoiceFilter
is used if you want to filter from a choice of another model. So in your case you would use regular ChoiceFilter
from .models import Period
class ContainerFilter(FilterSet):
type_of_period = ChoiceFilter(choices=Period.PeriodType)
class Meta:
model = models.Container
fields = ['type_of_period',]
In the docs however, they are defining the choices like so:
STATUS_CHOICES = (
(0, 'Regular'),
(1, 'Manager'),
(2, 'Admin'),
)
And not,
class PeriodType(models.TextChoices):
LONG = 'long', 'Long period'
SHORT = 'short', 'Short period'
So if it doesn’t work maybe you have to replace PeriodType
with:
PERIOD_CHOICES = (
('long', 'Long period'),
('short', 'Short period'),
)
Source:stackexchange.com