mirror of
https://github.com/jazzband/django-admin2.git
synced 2026-03-16 22:20:24 +00:00
allow sort by field
This commit is contained in:
parent
153137d4eb
commit
06c2e81392
4 changed files with 51 additions and 5 deletions
9
djadmin2/static/themes/bootstrap/css/base.css
Normal file
9
djadmin2/static/themes/bootstrap/css/base.css
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
.sort_link {
|
||||
display: block;
|
||||
cursor: pointer;
|
||||
color: black;
|
||||
}
|
||||
|
||||
.sort_link:hover {
|
||||
color: black;
|
||||
}
|
||||
|
|
@ -9,6 +9,7 @@
|
|||
{% block css %}
|
||||
<link href="{{ STATIC_URL }}themes/bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
|
||||
<link href="{{ STATIC_URL }}themes/bootstrap/css/bootstrap-custom.css" rel="stylesheet" media="screen">
|
||||
<link href="{{ STATIC_URL }}themes/bootstrap/css/base.css" rel="stylesheet" media="screen">
|
||||
{% endblock css %}
|
||||
</head>
|
||||
<body>
|
||||
|
|
|
|||
|
|
@ -70,11 +70,22 @@
|
|||
<thead>
|
||||
<th class="checkbox-column"><input type="checkbox" class="model-select-all"></th>
|
||||
{% for attr in view.model_admin.list_display %}
|
||||
{% if forloop.first and attr == "__str__" %}
|
||||
<th>{{ model_name|capfirst }}</th>
|
||||
{% else %}
|
||||
<th>{{ model|model_attr_verbose_name:attr|capfirst }}</th>
|
||||
{% endif%}
|
||||
<th>
|
||||
{# comment if we sorted on this field last time invert the sort #}
|
||||
{% if sort_term == attr %}
|
||||
<a class='sort_link' href='./?sort=-{{attr}}'>
|
||||
{% else %}
|
||||
<a class='sort_link' href='./?sort={{attr}}'>
|
||||
{% endif %}
|
||||
|
||||
{% if forloop.first and attr == "__str__" %}
|
||||
{{ model_name|capfirst }}
|
||||
{% else %}
|
||||
{{ model|model_attr_verbose_name:attr|capfirst }}
|
||||
{% endif %}
|
||||
|
||||
</a>
|
||||
</th>
|
||||
{% endfor %}
|
||||
</thead>
|
||||
<tbody>
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ from django.http import HttpResponseRedirect
|
|||
from django.utils.encoding import force_text
|
||||
from django.utils.text import capfirst
|
||||
from django.views import generic
|
||||
from django.db.models.fields import FieldDoesNotExist
|
||||
|
||||
import extra_views
|
||||
|
||||
|
|
@ -134,11 +135,34 @@ class ModelListView(AdminModel2Mixin, generic.ListView):
|
|||
if self.model_admin.list_filter:
|
||||
queryset = self.build_list_filter(queryset).qs
|
||||
|
||||
queryset = self._modify_queryset_for_sort(queryset)
|
||||
|
||||
if search_use_distinct:
|
||||
return queryset.distinct()
|
||||
else:
|
||||
return queryset
|
||||
|
||||
def _modify_queryset_for_sort(self, queryset):
|
||||
# If we are sorting AND the field exists on the model
|
||||
sort_by = self.request.GET.get('sort', None)
|
||||
if sort_by:
|
||||
# Special case when we are not explicityly displaying fields
|
||||
if sort_by == '-__str__':
|
||||
queryset = queryset[::-1]
|
||||
try:
|
||||
# If we sort on '-' remove it before looking for that field
|
||||
field_exists = sort_by
|
||||
if field_exists[0] == '-':
|
||||
field_exists = field_exists[1:]
|
||||
|
||||
options = utils.model_options(self.model)
|
||||
options.get_field(field_exists)
|
||||
queryset = queryset.order_by(sort_by)
|
||||
except FieldDoesNotExist:
|
||||
# If the field does not exist then we dont sort on it
|
||||
pass
|
||||
return queryset
|
||||
|
||||
def build_list_filter(self, queryset=None):
|
||||
if not hasattr(self, '_list_filter'):
|
||||
if queryset is None:
|
||||
|
|
@ -157,6 +181,7 @@ class ModelListView(AdminModel2Mixin, generic.ListView):
|
|||
context['search_fields'] = self.get_search_fields()
|
||||
context['search_term'] = self.request.GET.get('q', '')
|
||||
context['list_filter'] = self.build_list_filter()
|
||||
context['sort_term'] = self.request.GET.get('sort', '')
|
||||
return context
|
||||
|
||||
def get_success_url(self):
|
||||
|
|
|
|||
Loading…
Reference in a new issue