@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
Closed this issue · 0 comments
Nagendra-babu77 commented
@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>
...
Originally posted by @robertpro in #51 (comment)