Python 这是什么;返回unicode(self.creator)和#x2B"E;”&引用+;自我介绍标题“;你住在Django吗?
我正在学习关于使用Django/Python创建论坛的lightbird教程。下面是创建Python 这是什么;返回unicode(self.creator)和#x2B"E;”&引用+;自我介绍标题“;你住在Django吗?,python,django,forum,Python,Django,Forum,我正在学习关于使用Django/Python创建论坛的lightbird教程。下面是创建线程模型的代码 class Thread(models.Model): title = models.CharField(max_length=100) created = models.DateTimeField(auto_now_add=True) creator = models.ForeignKey(User, blank=True, null=True) modifi
线程
模型的代码
class Thread(models.Model):
title = models.CharField(max_length=100)
created = models.DateTimeField(auto_now_add=True)
creator = models.ForeignKey(User, blank=True, null=True)
modified = models.DateTimeField(auto_now=True)
forum = models.ForeignKey(Forum)
def __unicode__(self):
return unicode(self.creator) + " - " + self.title
和Post
模型:
class Post(models.Model):
title = models.CharField(max_length=60)
created = models.DateTimeField(auto_now_add=True)
creator = models.ForeignKey(User, blank=True, null=True)
thread = models.ForeignKey(Thread)
body = models.TextField(max_length=10000)
def __unicode__(self):
return u"%s - %s - %s" % (self.creator, self.thread, self.title)
def short(self):
return u"%s - %s\n%s" % (self.creator, self.title, self.created.strftime("%b %d, %I:%M %p"))
short.allow_tags = True
使用unicode函数后,我很难理解代码!我一直在使用unicode,同时以非常简单的形式创建模型,如:
class Post(models.Model):
title = models.CharField(max_length=100)
def __unicode__(self):
return self.title
我理解这一点,但不理解上面模型中的代码。有人能给我解释一下吗。谢谢大家!
unicode(self.creator) +\ #will call the __unicode__ method of the User class
' - ' +\ # will add a dash
self.title #will add the title which is a string
那么第二个呢
"%s"%some_var #will convert some_var to a string (call __str__ usually...may fall back on __unicode__ or something)
所以
将调用创建者的用户类上的\uuuuuuu str\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
然后添加一个破折号和标题
\n
是一个结束行
并且strftime
将时间戳转换为英语“MonthAbbrv.Day,24Hr:Minutes”
return u"%s - %s\n%s" % (self.creator, self.title, self.created.strftime("%b %d, %I:%M %p"))