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

时间:2021-08-02 20:15:39

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?

我是全新的django和sql。我正在经历的一个教程在每个模型类中放置def __unicode __(self)?这里是对文档的引用但是即使在阅读之后我还是不明白它的目的是什么?

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

1 个解决方案

#1


6  

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

这个想法是当你打印{{Project}}时,你基本上只是获得了一堆并不真正提供信息的垃圾。

def __Unicode__(self):

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

这定义了您打印的内容,因此{{Project}}将显示对象的描述。对你来说哪个更有用。

#1


6  

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

这个想法是当你打印{{Project}}时,你基本上只是获得了一堆并不真正提供信息的垃圾。

def __Unicode__(self):

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

这定义了您打印的内容,因此{{Project}}将显示对象的描述。对你来说哪个更有用。