I’m trying to create a filter for DRF viewset that will filter my model field:
is_claim_submitted = models.BooleanField(
IS_CLAIM_SUBMITTED_NAME, blank=True, null=True
)
I’ve written a filter according to django-filter docs
class BoolInFilter(BaseInFilter, BooleanFilter):
pass
class DefectFilter(django_filters.FilterSet):
is_claim_submitted__in = BoolInFilter(
field_name="is_claim_submitted",
lookup_expr="in",
)
class Meta:
model = Defect
fields = {
"id",
"is_claim_submitted",
}
So I’m trying to filter a queryset by sending request http://localhost:8000/api/v1/defects/is_claim_submitted__in=true,false
. Basicly, the values of URI param must be in a list of [True, False, None]. How ever it doesn’t filter and it doesn’t parse the values to python bools (true -> True). For some reason it doesn’t get field_class = forms.NullBooleanField
from BooleanFilter
, but uses default Field
parsing.
I’ve already checked the MRO, though I still can’t manage to figure out why my code doesn’t work.
The problem is precisely with BooleanFilter, as all the same but with NumberFilter and ids works fine.
Is there a mistake in my code or is there a better way to implement such a filter?