Change: Optional Unicode Return Values

created on Dec. 15, 2012, 9:33 a.m. by Hevok & updated on Dec. 15, 2012, 9:33 a.m. by Hevok

The __unicode__() method of a model can return optional values. For instance, if the values need to be returned from other models.

.. sourcecode:: python

def __unicode__(self):
    return (u'%s%s%s') % (self.target, ('|' if self.page_type else ''), (self.page_type if self.page_type else ''))

Alternatively:

.. sourecode:: python

def __unicode__(self):
    ";".join([unicode(x) for x in (thing_a, thing_b, getattr(obj, "optional_attribute", None), ...etc...) if x is not None])
values.jpg

Tags: rest, django, models
Categories: Tutorial, reST
Parent: Web Framework

Comment: Created entry.

See entry | Admin

Comment on This Data Unit