Changeset 4309
- Timestamp:
- 01/12/07 13:40:06 (2 years ago)
- Files:
-
- django/trunk/django/contrib/admin/templatetags/admin_list.py (modified) (3 diffs)
- django/trunk/docs/model-api.txt (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/contrib/admin/templatetags/admin_list.py
r4265 r4309 101 101 "url": cl.get_query_string({ORDER_VAR: i, ORDER_TYPE_VAR: new_order_type}), 102 102 "class_attrib": (th_classes and ' class="%s"' % ' '.join(th_classes) or '')} 103 104 def _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) 103 107 104 108 def items_for_result(cl, result): … … 115 119 attr = getattr(result, field_name) 116 120 allow_tags = getattr(attr, 'allow_tags', False) 121 boolean = getattr(attr, 'boolean', False) 117 122 if callable(attr): 118 123 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) 120 129 except (AttributeError, ObjectDoesNotExist): 121 130 result_repr = EMPTY_CHANGELIST_VALUE … … 148 157 # Booleans are special: We use images. 149 158 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) 152 160 # FloatFields are special: Zero-pad the decimals. 153 161 elif isinstance(f, models.FloatField): django/trunk/docs/model-api.txt
r4276 r4309 1268 1268 return '<span style="color: #%s;">%s %s</span>' % (self.color_code, self.first_name, self.last_name) 1269 1269 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 1270 1288 1271 1289 * The ``__str__()`` method is just as valid in ``list_display`` as any
