Posts

Showing posts with the label Filters

order_by() method in Django

Order results by different fields using the order_by() method of the manager. For Ex:, you can retrieve all objects ordered by their post_name, as follows: Posts.objects.order_by('post_name') Ascending order is implied. You can indicate descending order with a negative sign prefix. Posts.objects.order_by('-post_name')

exclude() method in Django

You can exclude certain results from the QuerySet using the exclude() method of the manager. For Ex:, you can retrieve all posts published in 2019 whose titles don't start with xyz: Posts.objects.filter(publish__year=2019).exclude(title__startswith='xyz') 

filter() method in Django

Use the filter() method to filter a  QuerySet . For Ex:, you can retrieve all posts created in the year 2019 using the following QuerySet: >> Posts.objects.filter(publish__year=2019) You can also filter by multiple fields. For Ex:, you can retrieve all posts published in 2019 by the author with the username abcd: >>> Posts.objects.filter(publish__year=2019, author__username='abcd')

FILTERS_DISABLE_HELP_TEXT in Django Filters

FILTERS_DISABLE_HELP_TEXT:- Some filters provide informational help_text.  For example, csv-based filters (filters.BaseCSVFilter) inform users that “Multiple values may be    separated by commas”. You can set this to True to disable the help_text for all filters by removing the text from the rendered form’s output.

FILTERS_NULL_CHOICE_VALUE in Django Filters

FILTERS_NULL_CHOICE_VALUE:- Set the default value for ChoiceFilter.null_value . You may want to change this value if the default 'null' string conflicts with an actual choice. Default value is 'null'

FILTERS_NULL_CHOICE_LABEL in Dango Filters

FILTERS_NULL_CHOICE_LABEL:- Set the default value for ChoiceFilter.null_label . You may enable the null choice by setting a non-None value. Default value is None.

FILTERS_EMPTY_CHOICE_LABEL in Djangi Filters

FILTERS_EMPTY_CHOICE_LABEL:- Set the default value for ChoiceFilter.empty_label . You may disable the empty choice by setting this to None. Default value is '---------'

FILTERS_DEFAULT_LOOKUP_EXPR in Django Filters

FILTERS_DEFAULT_LOOKUP_EXPR:- Set the default lookup expression to be generated, when none is defined. Default value is 'exact'. 

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...

BooleanFilter in Django

BooleanFilter filter matches a boolean, either True or False, used with BooleanField and NullBooleanField by default. Example: boolean_field_filter = filters.BooleanFilter(field_name='boolean_field_filter', label='Boolean Field Filter')     

UUIDFilter in Django

  UUIDFilter filter matches UUID values, used with models. UUIDField by default.

CharFilter in Django

  CharFilter filter does simple character matches, used with CharField and TextField by default. ex: char_filter_field_name = CharFilter(field_name='char_filter_field_name', lookup_expr='icontains', label='Char Filter Field Name', widget=forms.TextInput(attrs={'class': 'col-md-12 mr-2','placeholder': 'Please Enter Char Filter Field Name', 'style': 'width: 263px'}))