Django实践:个人博客系统(第七章 Model的设计和使用)

时间:2022-04-07 20:29:37

model中定义一个类,如果不在Meta中指明verbose_name,在admin页面中默认显示类名。如:

class Publisher(models.Model):
name
= models.CharField(max_length=30)
address
= models.CharField(max_length=50, blank=True)
city
= models.CharField(max_length=60)
state_province
= models.CharField(max_length=30, blank=True)
country
= models.CharField(max_length=50)
website
= models.URLField(blank=True)

class Meta:
verbose_name
= '出版者'
verbose_name_plural
= verbose_name

def __unicode__(self):
return self.name


class Author(models.Model):
first_name
= models.CharField(max_length=30)
last_name
= models.CharField(max_length=40)
email
= models.EmailField()

class Meta:
verbose_name
= '作者'
verbose_name_plural
= verbose_name

def __unicode__(self):
return self.first_name


class Book(models.Model):
title
= models.CharField(max_length=100)
authors
= models.ManyToManyField(Author, blank=True)
publisher
= models.ForeignKey(Publisher, blank=True)
publication_date
= models.DateField(blank=True)

#class Meta:
# verbose_name = '书名'
# verbose_name_plural = verbose_name

def __unicode__(self):
return self.title

在admin中显示情况:Django实践:个人博客系统(第七章 Model的设计和使用)

在字段中的verbose_name应该同理,如:

class Publisher(models.Model):
name
= models.CharField(max_length=30,verbose_name = '姓名')
address
= models.CharField(max_length=50, blank=True)

此时一个Publisher的实例在admin中显示的name字段应该为verbose_name的值姓名,但在这里代码中verbose_name并没有写。

Django实践:个人博客系统(第七章 Model的设计和使用)