Code

Ticket #11195: 11195_modeladmin_change_list_classes.patch

File 11195_modeladmin_change_list_classes.patch, 4.3 KB (added by vdboor, 3 years ago)

Add a change_list_classes dictionary to the ModelAdmin class.

  • django/contrib/admin/options.py

    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): 
    264264 
    265265    list_display = ('__str__',) 
    266266    list_display_links = () 
     267    list_column_classes = {} 
    267268    list_filter = () 
    268269    list_select_related = False 
    269270    list_per_page = 100 
    class ModelAdmin(BaseModelAdmin): 
    10641065        ChangeList = self.get_changelist(request) 
    10651066        try: 
    10661067            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, 
    10681069                self.list_select_related, self.list_per_page, self.list_editable, self) 
    10691070        except IncorrectLookupParameters: 
    10701071            # Wacky lookup parameters were given, so redirect to the main 
  • django/contrib/admin/templatetags/admin_list.py

    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): 
    136136    pk = cl.lookup_opts.pk.attname 
    137137    for field_name in cl.list_display: 
    138138        row_class = '' 
     139        row_classes = [] 
    139140        try: 
    140141            f, attr, value = lookup_field(field_name, result, cl.model_admin) 
    141142        except (AttributeError, ObjectDoesNotExist): 
    def items_for_result(cl, result, form): 
    143144        else: 
    144145            if f is None: 
    145146                if field_name == u'action_checkbox': 
    146                     row_class = ' class="action-checkbox"' 
     147                    row_classes = ['action-checkbox'] 
    147148                allow_tags = getattr(attr, 'allow_tags', False) 
    148149                boolean = getattr(attr, 'boolean', False) 
    149150                if boolean: 
    def items_for_result(cl, result, form): 
    169170                if isinstance(f, models.DateField)\ 
    170171                or isinstance(f, models.TimeField)\ 
    171172                or isinstance(f, models.ForeignKey): 
    172                     row_class = ' class="nowrap"' 
     173                    row_classes.append('nowrap') 
    173174        if force_unicode(result_repr) == '': 
    174175            result_repr = mark_safe('&nbsp;') 
     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) 
    175181        # If list_display_links not defined, add the link tag to the first field 
    176182        if (first and not cl.list_display_links) or field_name in cl.list_display_links: 
    177183            table_tag = {True:'th', False:'td'}[first] 
  • django/contrib/admin/views/main.py

    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' 
    2727EMPTY_CHANGELIST_VALUE = ugettext_lazy('(None)') 
    2828 
    2929class 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): 
    3131        self.model = model 
    3232        self.opts = model._meta 
    3333        self.lookup_opts = self.opts 
    3434        self.root_query_set = model_admin.queryset(request) 
    3535        self.list_display = list_display 
    3636        self.list_display_links = list_display_links 
     37        self.list_column_classes = list_column_classes 
    3738        self.list_filter = list_filter 
    3839        self.date_hierarchy = date_hierarchy 
    3940        self.search_fields = search_fields