在django admin中更改列名

时间:2014-06-03 12:52:43

标签: python django

在django管理站点中,我看到我的模型字段名称很短,如dob(出生日期等)。如何自定义管理页面以显示自定义字段名称。我的模型和admin.py如下,我搜索并发现我必须使用简短的描述,但不确定确切的使用位置或如何实现它。

class Project(models.Model):
        name = models.CharField(max_length = 100)
        description = models.CharField(max_length = 254)

        def __unicode__(self):
             return u"%s" % (self.name)

class Employee(models.Model):
     name = models.CharField(max_length = 100)
     dob = models.DateField()
     doj = models.DateField()
         mobile=models.IntegerField(max_length = 12)
         email = models.EmailField(max_length=50)
     bill = models.BooleanField(db_index=True,default=False)
     proj = models.ForeignKey(Project)

        def custom_field_name(self):
          return self.dob
        custom_field_name.short_description = 'Date Of Birth'

     def __unicode__(self):
        return u"%s" % (self.name)

admin.py如下,

 class EmployeeAdmin(admin.ModelAdmin):
    search_fields = ['name']
        list_display = ('name','bill','mobile','email')
        list_filter = ('bill','proj')

admin.site.register( Employee, EmployeeAdmin )
admin.site.register( Project )

2 个答案:

答案 0 :(得分:2)

您可以在模型中设置字段的显示名称(https://docs.djangoproject.com/en/dev/topics/db/models/#verbose-field-names

class Employee(models.Model):
     name = models.CharField(max_length = 100)
     dob = models.DateField('date of birth')
     doj = models.DateField()
     mobile=models.IntegerField(max_length = 12)
     email = models.EmailField(max_length=50)
     bill = models.BooleanField(db_index=True,default=False)
     proj = models.ForeignKey(Project, verbose_name='project')

这样做意味着此模型的所有表单(在管理员和站点前端)都会显示您输入的文本作为字段标签。

答案 1 :(得分:0)

https://docs.djangoproject.com/en/dev/intro/tutorial02/

class Question(models.Model):
    # ...
    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
    was_published_recently.admin_order_field = 'pub_date'
    was_published_recently.boolean = True
    was_published_recently.short_description = 'Published recently?'

如上所述:How to change Django Admin Custom list field label