Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use __unicode__(self) method for django 1.7+? [closed]

I'm brand new with django and sql. A tutorial I'm going through puts def __unicode__(self) on every models class? here's the reference to the documentation But even after reading I still do not understand whats the purpose?

class Project(models.Model):
    name = models.CharField(max_length=300)

    def __unicode__(self):
        return self.name

class Task(models.Model):
    description = models.CharField(max_length=300)
    project = models.ForeignKey(Project)

    def __unicode__(self):
        return self.description
like image 473
Armeen Harwood Avatar asked Aug 16 '15 01:08

Armeen Harwood


1 Answers

The idea is when you print {{Project}} you get essentially just get a bunch of garbage that isn't really informative.

def __Unicode__(self):

This defines what you print so {{Project}} would display the description of the object. Which is much more useful, TO YOU.

like image 90
Luke Xu Avatar answered Nov 09 '22 10:11

Luke Xu