Ticket #7503: list_display_takes_function.patch

File list_display_takes_function.patch, 2.3 KB (added by qmanic@…, 16 years ago)
  • django/contrib/admin/templatetags/admin_list.py

     
    7676            f = lookup_opts.get_field(field_name)
    7777            admin_order_field = None
    7878        except models.FieldDoesNotExist:
     79            attr = None
    7980            # For non-field list_display values, check for the function
    8081            # attribute "short_description". If that doesn't exist, fall back
    8182            # to the method name. And __str__ and __unicode__ are special-cases.
     
    8485            elif field_name == '__str__':
    8586                header = smart_str(lookup_opts.verbose_name)
    8687            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                   
    8893                try:
    8994                    header = attr.short_description
    9095                except AttributeError:
    9196                    header = field_name.replace('_', ' ')
    9297
    9398            # 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)
    95102            if not admin_order_field:
    96103                yield {"text": header}
    97104                continue
     
    141148                    result_repr = _boolean_icon(attr)
    142149                else:
    143150                    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
    144158            except (AttributeError, ObjectDoesNotExist):
    145159                result_repr = EMPTY_CHANGELIST_VALUE
    146160            else:
Back to Top