Ticket #7503: list_display_takes_function.patch
File list_display_takes_function.patch, 2.3 KB (added by , 16 years ago) |
---|
-
django/contrib/admin/templatetags/admin_list.py
76 76 f = lookup_opts.get_field(field_name) 77 77 admin_order_field = None 78 78 except models.FieldDoesNotExist: 79 attr = None 79 80 # For non-field list_display values, check for the function 80 81 # attribute "short_description". If that doesn't exist, fall back 81 82 # to the method name. And __str__ and __unicode__ are special-cases. … … 84 85 elif field_name == '__str__': 85 86 header = smart_str(lookup_opts.verbose_name) 86 87 else: 87 attr = getattr(cl.model, field_name) # Let AttributeErrors propagate. 88 if callable(field_name): # field_name may be a function 89 attr = field_name 90 else: 91 attr = getattr(cl.model, field_name) # Let AttributeErrors propagate. 92 88 93 try: 89 94 header = attr.short_description 90 95 except AttributeError: 91 96 header = field_name.replace('_', ' ') 92 97 93 98 # It is a non-field, but perhaps one that is sortable 94 admin_order_field = getattr(getattr(cl.model, field_name), "admin_order_field", None) 99 if not attr: 100 attr = getattr(cl.model, field_name) 101 admin_order_field = getattr(attr, "admin_order_field", None) 95 102 if not admin_order_field: 96 103 yield {"text": header} 97 104 continue … … 141 148 result_repr = _boolean_icon(attr) 142 149 else: 143 150 result_repr = smart_unicode(attr) 151 except TypeError: 152 # if field_name is a callable, evaluate it passing result as 153 # first argument 154 if callable(field_name): 155 result_repr = field_name(result) 156 else: 157 raise 144 158 except (AttributeError, ObjectDoesNotExist): 145 159 result_repr = EMPTY_CHANGELIST_VALUE 146 160 else: