silentsokolov / django-admin-rangefilter

A Django app that lets you filter data by date range and numeric range in the admin UI
MIT License
713 stars 106 forks source link

@vekerdyb This is the code I will use: Not working for me.. got object has no attribute self.lookup_kwarg_isnull.. pls attach detailed code how to use it #102

Closed Nagendra-babu77 closed 9 months ago

Nagendra-babu77 commented 1 year ago
          @vekerdyb This is the code I will use:
class DateRangeListFilter(admin.DateFieldListFilter, DateRangeFilter):
    template = 'admin/filters/date_filter.html'

    def expected_parameters(self):
        params = [self.lookup_kwarg_gte, self.lookup_kwarg_lte]
        if self.field.null:
            params.append(self.lookup_kwarg_isnull)
        return params

    def _make_query_filter(self, request, validated_data):
        query_params = super()._make_query_filter(request, validated_data)
        date_value_gte = validated_data.get(self.lookup_kwarg_gte, None)
        date_value_lte = validated_data.get(self.lookup_kwarg_lte, None)
        date_value_isnull = validated_data.get(self.lookup_kwarg_isnull, None)

        if date_value_isnull is not None and not any([date_value_lte, date_value_gte]):
            query_params[self.lookup_kwarg_isnull] = date_value_isnull

        return query_params

    def _get_form_fields(self):
        fields = super()._get_form_fields()
        fields.update(OrderedDict(
            (
                (self.lookup_kwarg_isnull, forms.BooleanField(
                    label='',
                    localize=True,
                    required=False,
                    widget=forms.HiddenInput,
                )),
            )
        ))
        return fields

Same as rangefilter/date_filter.html, but inserting Django admin/filter.html here, which is this code in my case.

...
<ul>
    {% for choice in choices %}
        <li{% if choice.selected %} class="selected"{% endif %}>
            <a href="{{ choice.query_string|iriencode }}" title="{{ choice.display }}">{{ choice.display }}</a>
        </li>
    {% endfor %}
</ul>
...

Result: image

Originally posted by @robertpro in https://github.com/silentsokolov/django-admin-rangefilter/issues/51#issuecomment-1023691186