可以重命名字段吗?
示例:
models.py
class Person(models.Model):
nameP = models.CharField(max_length=40)
ageP = models.CharField(max_length=40)
def __unicode__(self):
return self.nameP
现在我想在我的addPerson.html中打印:
姓名:人1 年龄:18岁
谢谢!
答案 0 :(得分:2)
您可以在字段参数中使用verbose_name
,如下所示:
nameP = models.CharField(max_length=40, verbose_name='Name')
ageP = models.CharField(max_length=40, verbose_name='Age')
...
您也可以在Model
课程以及Meta
课程中执行此操作。假设您的Person
类实际上名为PPPerson
,如下所示:
class PPPerson(models.Model):
nameP = models.CharField(max_length=40, verbose_name='Name')
ageP = models.CharField(max_length=40, verbose_name='Age')
class Meta:
verbose_name = 'Person'
verbose_name_plural = 'People'
答案 1 :(得分:0)
使用`verbose_name'参数:
class Person(models.Model):
nameP = models.CharField(max_length=40, verbose_name='Name')
ageP = models.CharField(max_length=40, verbose_name='Age')
def __unicode__(self):
return self.nameP
答案 2 :(得分:0)
您可以重命名模型中的字段:
$ python manage.py makemigrations --empty myApp
migrations.RenameField('MyModel', 'old_field_name', 'new_field_name')
$ python manage.py migrate myApp