Django

Code

Changeset 4309

Show
Ignore:
Timestamp:
01/12/07 13:40:06 (2 years ago)
Author:
jacob
Message:

Fixed #3287: method columns in the admin changelist can now have a boolean attribute that uses the clever little checkmark icons instead of the ugly "True" or "False" strings. Thanks for the patch, xian@mintchaos.com

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/contrib/admin/templatetags/admin_list.py

    r4265 r4309  
    101101                       "url": cl.get_query_string({ORDER_VAR: i, ORDER_TYPE_VAR: new_order_type}), 
    102102                       "class_attrib": (th_classes and ' class="%s"' % ' '.join(th_classes) or '')} 
     103 
     104def _boolean_icon(field_val): 
     105    BOOLEAN_MAPPING = {True: 'yes', False: 'no', None: 'unknown'} 
     106    return '<img src="%simg/admin/icon-%s.gif" alt="%s" />' % (settings.ADMIN_MEDIA_PREFIX, BOOLEAN_MAPPING[field_val], field_val) 
    103107 
    104108def items_for_result(cl, result): 
     
    115119                attr = getattr(result, field_name) 
    116120                allow_tags = getattr(attr, 'allow_tags', False) 
     121                boolean = getattr(attr, 'boolean', False) 
    117122                if callable(attr): 
    118123                    attr = attr() 
    119                 result_repr = str(attr) 
     124                if boolean: 
     125                    allow_tags = True 
     126                    result_repr = _boolean_icon(attr) 
     127                else: 
     128                    result_repr = str(attr) 
    120129            except (AttributeError, ObjectDoesNotExist): 
    121130                result_repr = EMPTY_CHANGELIST_VALUE 
     
    148157            # Booleans are special: We use images. 
    149158            elif isinstance(f, models.BooleanField) or isinstance(f, models.NullBooleanField): 
    150                 BOOLEAN_MAPPING = {True: 'yes', False: 'no', None: 'unknown'} 
    151                 result_repr = '<img src="%simg/admin/icon-%s.gif" alt="%s" />' % (settings.ADMIN_MEDIA_PREFIX, BOOLEAN_MAPPING[field_val], field_val) 
     159                result_repr = _boolean_icon(field_val) 
    152160            # FloatFields are special: Zero-pad the decimals. 
    153161            elif isinstance(f, models.FloatField): 
  • django/trunk/docs/model-api.txt

    r4276 r4309  
    12681268                  return '<span style="color: #%s;">%s %s</span>' % (self.color_code, self.first_name, self.last_name) 
    12691269              colored_name.allow_tags = True 
     1270 
     1271    * If the string given is a method of the model that returns True or False  
     1272      Django will display a pretty "on" or "off" icon if you give the method a  
     1273      ``boolean`` attribute whose value is ``True``. 
     1274 
     1275      Here's a full example model:: 
     1276 
     1277          class Person(models.Model): 
     1278              first_name = models.CharField(maxlength=50) 
     1279              birthday = models.DateField() 
     1280 
     1281              class Admin: 
     1282                  list_display = ('name', 'born_in_fifties') 
     1283 
     1284              def born_in_fifties(self): 
     1285                  return self.birthday.strftime('%Y')[:3] == 5 
     1286              born_in_fifties.boolean = True 
     1287 
    12701288 
    12711289    * The ``__str__()`` method is just as valid in ``list_display`` as any