Django admin filter by ranges of ages
Adds filtering by ranges of dates birth in the admin filter sidebar
class RangeValuesFilterSpec(FilterSpec):
def __init__(self, f, request, params, model, model_admin):
super(RangeValuesFilterSpec, self).__init__(f, request, params, model, model_admin)
self.field_generic = '%s__' % self.field.name
self.parsed_params = dict([(k, v) for k, v in params.items() if k.startswith(self.field_generic)])
self.links = [(_('All'), {})]
last_value = None
for max_value in sorted(f.list_filter_range):
max_value = str(max_value)
if last_value == None:
label = '< ' + max_value
range = {'%s__lt' % f.name: max_value}
else:
label = last_value + ' - ' + max_value
range = {'%s__gte' % self.field.name: last_value, '%s__lt' % f.name: max_value}
self.links.append((_(mark_safe(label)), range))
last_value = max_value
self.links.append((_(mark_safe('≥ ' + max_value)), {'%s__gte' % f.name: max_value}))
def choices(self, cl):
for title, param_dict in self.links:
yield {'selected': self.parsed_params == param_dict,
'query_string': cl.get_query_string(param_dict, [self.field_generic]),
'display': title}
https://github.com/coolchevy/django-rangevaluesfilterspec
Читайте також:
- Ordering related objects in Django
- django postgresql та SCHEMA для таблиць
- MemcachedKeyLengthError: Key length is > 250
- Django yandex and google maps integration
- Fix Django 1.4 admin_site ForeignKeyRawIdWidget issue
- django-stdimage traceback "The '%s' attribute has no file associated with it"
- Use custom admin filters in ModelAdmin
- Python + CouchDB
- Django admin filter by ranges of dates with calendar
- Django suit admin inline pagination