Django Methods: __unicode__()

By: Dusty Arlia
Published on Monday, February 24, 2014, 07:16 PM
Last Updated on Friday, July 03, 2015 at 12:04 PM
Total Updates: 2

It is common to define a __unicode__() method in your model. Here is an example:

from django.db import models

class Person(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)

    def __unicode__(self):
        return u'%s %s' % (self.first_name, self.last_name)

The __unicode__() method is called whenever you call the unicode() function on an object (unicode(obj)). The __unicode__() method should be used instead of __str__(). __unicode__() lets Django take care of the conversion to string objects when required.

Comments:

Ad:

Ad: