-
Notifications
You must be signed in to change notification settings - Fork 101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add custom predefined date ranges #51
Comments
I would like to mix both, date range + no date + has date, any suggestion @wlisesrivas ? |
@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>
... |
Any plans to include this in the package? @silentsokolov |
Not working for me.. got object has no attribute self.lookup_kwarg_isnull |
@Nagendra-babu77 , there is a bug on my snippet, use this one: class DateRangeListFilter(admin.DateFieldListFilter, DateRangeFilter):
template = 'admin/filters/date_range_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()
if self.field.null:
fields.update(OrderedDict(
(
(self.lookup_kwarg_isnull, forms.BooleanField(
label='',
localize=True,
required=False,
widget=forms.HiddenInput,
)),
)
))
return fields @silentsokolov I have used this code for more than a year, please let me know if it looks good to add it in a PR |
Looks good. |
Is it possible to add quick-selections such as "This month", "Last month", "Next month", "This week", ...?
The text was updated successfully, but these errors were encountered: