ChoiceFilter in Django

ChoiceFilter filter matches values in its choices argument. The choices must be explicitly passed when the filter is declared on the FilterSet.

Example:

class User(models.Model):

STATUS_CHOICES = (

(0, 'Active'),

(1, 'Inactive'),

)

username = models.CharField(max_length=300)

firstname = SubCharField(max_length=150)

lastname = SubSubCharField(max_length=150)

user_status = models.IntegerField(choices=STATUS_CHOICES, default=0)

    class User_Filter(FilterSet):

         user_status = ChoiceFilter(choices=STATUS_CHOICES)

    class Meta:

         model = User

        fields = ['user_status']

  • ChoiceFilter also has arguments that enable a choice for not filtering, as well as a choice for filtering by None values.
      • empty_label: The display label to use for the select choice to not filter. The choice may be disabled by setting this argument to None. Defaults to FILTERS_EMPTY_CHOICE_LABEL.
      • null_label: The display label to use for the choice to filter by None values. The choice may be disabled by setting this argument to None. Defaults to FILTERS_NULL_CHOICE_LABEL.
      • null_value: The special value to match to enable filtering by None values. This value defaults FILTERS_NULL_CHOICE_VALUE and needs to be a non-empty value ('', None, [], (), {}).

    Comments

    Popular posts from this blog

    Internationalization (Language Setting) in Django

    reverse_lazy() Method in Django

    CharFilter in Django