commit 46c0cab480a348eae2b607f80e3e99064f828ed1
Author: Diederik van der Boor <vdboor@edoburu.nl>
Date: Fri Apr 22 15:36:18 2011 +0200
Add list_column_classes option to ModelAdmin.
This allows developers to easily set custom CSS classes
for a model, without having to overwrite half of the
`templatetags/admin_list.py` code
diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
index d08e22b..43ae8fe 100644
a
|
b
|
class ModelAdmin(BaseModelAdmin):
|
264 | 264 | |
265 | 265 | list_display = ('__str__',) |
266 | 266 | list_display_links = () |
| 267 | list_column_classes = {} |
267 | 268 | list_filter = () |
268 | 269 | list_select_related = False |
269 | 270 | list_per_page = 100 |
… |
… |
class ModelAdmin(BaseModelAdmin):
|
1064 | 1065 | ChangeList = self.get_changelist(request) |
1065 | 1066 | try: |
1066 | 1067 | cl = ChangeList(request, self.model, list_display, self.list_display_links, |
1067 | | self.list_filter, self.date_hierarchy, self.search_fields, |
| 1068 | self.list_column_classes, self.list_filter, self.date_hierarchy, self.search_fields, |
1068 | 1069 | self.list_select_related, self.list_per_page, self.list_editable, self) |
1069 | 1070 | except IncorrectLookupParameters: |
1070 | 1071 | # Wacky lookup parameters were given, so redirect to the main |
diff --git a/django/contrib/admin/templatetags/admin_list.py b/django/contrib/admin/templatetags/admin_list.py
index 139c0a1..0a71f63 100644
a
|
b
|
def items_for_result(cl, result, form):
|
136 | 136 | pk = cl.lookup_opts.pk.attname |
137 | 137 | for field_name in cl.list_display: |
138 | 138 | row_class = '' |
| 139 | row_classes = [] |
139 | 140 | try: |
140 | 141 | f, attr, value = lookup_field(field_name, result, cl.model_admin) |
141 | 142 | except (AttributeError, ObjectDoesNotExist): |
… |
… |
def items_for_result(cl, result, form):
|
143 | 144 | else: |
144 | 145 | if f is None: |
145 | 146 | if field_name == u'action_checkbox': |
146 | | row_class = ' class="action-checkbox"' |
| 147 | row_classes = ['action-checkbox'] |
147 | 148 | allow_tags = getattr(attr, 'allow_tags', False) |
148 | 149 | boolean = getattr(attr, 'boolean', False) |
149 | 150 | if boolean: |
… |
… |
def items_for_result(cl, result, form):
|
169 | 170 | if isinstance(f, models.DateField)\ |
170 | 171 | or isinstance(f, models.TimeField)\ |
171 | 172 | or isinstance(f, models.ForeignKey): |
172 | | row_class = ' class="nowrap"' |
| 173 | row_classes.append('nowrap') |
173 | 174 | if force_unicode(result_repr) == '': |
174 | 175 | result_repr = mark_safe(' ') |
| 176 | column_class = cl.list_column_classes.get(field_name) |
| 177 | if column_class: |
| 178 | row_classes.append(column_class) |
| 179 | if row_classes: |
| 180 | row_class = ' class="%s"' % ' '.join(row_classes) |
175 | 181 | # If list_display_links not defined, add the link tag to the first field |
176 | 182 | if (first and not cl.list_display_links) or field_name in cl.list_display_links: |
177 | 183 | table_tag = {True:'th', False:'td'}[first] |
diff --git a/django/contrib/admin/views/main.py b/django/contrib/admin/views/main.py
index 170d168..741179c 100644
a
|
b
|
ERROR_FLAG = 'e'
|
27 | 27 | EMPTY_CHANGELIST_VALUE = ugettext_lazy('(None)') |
28 | 28 | |
29 | 29 | class ChangeList(object): |
30 | | def __init__(self, request, model, list_display, list_display_links, list_filter, date_hierarchy, search_fields, list_select_related, list_per_page, list_editable, model_admin): |
| 30 | def __init__(self, request, model, list_display, list_display_links, list_column_classes, list_filter, date_hierarchy, search_fields, list_select_related, list_per_page, list_editable, model_admin): |
31 | 31 | self.model = model |
32 | 32 | self.opts = model._meta |
33 | 33 | self.lookup_opts = self.opts |
34 | 34 | self.root_query_set = model_admin.queryset(request) |
35 | 35 | self.list_display = list_display |
36 | 36 | self.list_display_links = list_display_links |
| 37 | self.list_column_classes = list_column_classes |
37 | 38 | self.list_filter = list_filter |
38 | 39 | self.date_hierarchy = date_hierarchy |
39 | 40 | self.search_fields = search_fields |