Author: jacob
Date: 2007-01-12 13:40:06 -0600 (Fri, 12 Jan 2007)
New Revision: 4309
Modified:
django/trunk/django/contrib/admin/templatetags/admin_list.py
django/trunk/docs/model-api.txt
Log:
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, [EMAIL PROTECTED]
Modified: django/trunk/django/contrib/admin/templatetags/admin_list.py
===================================================================
--- django/trunk/django/contrib/admin/templatetags/admin_list.py
2007-01-12 15:16:55 UTC (rev 4308)
+++ django/trunk/django/contrib/admin/templatetags/admin_list.py
2007-01-12 19:40:06 UTC (rev 4309)
@@ -101,6 +101,10 @@
"url": cl.get_query_string({ORDER_VAR: i,
ORDER_TYPE_VAR: new_order_type}),
"class_attrib": (th_classes and ' class="%s"' % '
'.join(th_classes) or '')}
+def _boolean_icon(field_val):
+ BOOLEAN_MAPPING = {True: 'yes', False: 'no', None: 'unknown'}
+ return '<img src="%simg/admin/icon-%s.gif" alt="%s" />' %
(settings.ADMIN_MEDIA_PREFIX, BOOLEAN_MAPPING[field_val], field_val)
+
def items_for_result(cl, result):
first = True
pk = cl.lookup_opts.pk.attname
@@ -114,9 +118,14 @@
try:
attr = getattr(result, field_name)
allow_tags = getattr(attr, 'allow_tags', False)
+ boolean = getattr(attr, 'boolean', False)
if callable(attr):
attr = attr()
- result_repr = str(attr)
+ if boolean:
+ allow_tags = True
+ result_repr = _boolean_icon(attr)
+ else:
+ result_repr = str(attr)
except (AttributeError, ObjectDoesNotExist):
result_repr = EMPTY_CHANGELIST_VALUE
else:
@@ -147,8 +156,7 @@
row_class = ' class="nowrap"'
# Booleans are special: We use images.
elif isinstance(f, models.BooleanField) or isinstance(f,
models.NullBooleanField):
- BOOLEAN_MAPPING = {True: 'yes', False: 'no', None: 'unknown'}
- result_repr = '<img src="%simg/admin/icon-%s.gif" alt="%s" />'
% (settings.ADMIN_MEDIA_PREFIX, BOOLEAN_MAPPING[field_val], field_val)
+ result_repr = _boolean_icon(field_val)
# FloatFields are special: Zero-pad the decimals.
elif isinstance(f, models.FloatField):
if field_val is not None:
Modified: django/trunk/docs/model-api.txt
===================================================================
--- django/trunk/docs/model-api.txt 2007-01-12 15:16:55 UTC (rev 4308)
+++ django/trunk/docs/model-api.txt 2007-01-12 19:40:06 UTC (rev 4309)
@@ -1268,6 +1268,24 @@
return '<span style="color: #%s;">%s %s</span>' %
(self.color_code, self.first_name, self.last_name)
colored_name.allow_tags = True
+ * If the string given is a method of the model that returns True or False
+ Django will display a pretty "on" or "off" icon if you give the method a
+ ``boolean`` attribute whose value is ``True``.
+
+ Here's a full example model::
+
+ class Person(models.Model):
+ first_name = models.CharField(maxlength=50)
+ birthday = models.DateField()
+
+ class Admin:
+ list_display = ('name', 'born_in_fifties')
+
+ def born_in_fifties(self):
+ return self.birthday.strftime('%Y')[:3] == 5
+ born_in_fifties.boolean = True
+
+
* The ``__str__()`` method is just as valid in ``list_display`` as any
other model method, so it's perfectly OK to do this::
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---