Skip to content

TypedChoiceFilter is **not** a superset of ChoiceFilter #1716

@JacKeelvar-O-Connor

Description

@JacKeelvar-O-Connor

Bug

The docs state that TypedChoiceFilter is the same as ChoiceFilter with an additional coerce argument https://django-filter.readthedocs.io/en/stable/ref/filters.html#typedchoicefilter. However, null_value and null_label are not acceptable arguments to TypedChoiceFilter.

Example snippet + error message

    myboolean = filters.TypedChoiceFilter(
        choices=(
            ("yes", "Yes"),
            ("no", "No"),
        ),
        coerce=lambda x: x == "yes",
        null_label="Unknown",
        null_value="null",
        field_name="myboolean",
    )
TypeError: Field.__init__() got an unexpected keyword argument 'null_label'

Relevant django-filters code

The following snippet implements the null lookup as best I can tell. It is implemented directly on the ChoiceFilter so is not shared with the TypedChoiceFilter (which does not inherit from ChoiceFilter).

def filter(self, qs, value):
if value != self.null_value:
return super().filter(qs, value)
qs = self.get_method(qs)(
**{"%s__%s" % (self.field_name, self.lookup_expr): None}
)
return qs.distinct() if self.distinct else qs

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions