可以在Django管理应用程序中更改字段标签吗?

时间:2022-01-07 20:18:06

As the title suggests. I want to be able to change the label of a single field in the admin application. I'm aware of the Form.field attribute, but how do I get my Model or ModelAdmin to pass along that information?

正如书名所暗示的。我希望能够更改管理应用程序中单个字段的标签。我知道这个表格。字段属性,但是如何让我的模型或模型管理员传递这些信息呢?

5 个解决方案

#1


60  

the verbose name of the field is the (optional) first parameter at field construction.

字段的详细名称是字段构造的第一个参数(可选)。

#2


20  

If your field is a property (a method) then you should use short_description:

如果字段是属性(方法),则应该使用short_description:

class Person(models.Model):
    ...

    def address_report(self, instance):
        ...
    # short_description functions like a model field's verbose_name
    address_report.short_description = "Address"

#3


13  

As Javier suggested you can use verbose name in your fields in model.py. Example as below,

正如Javier所建议的,您可以在model.py的字段中使用详细名称。例子如下,

class Employee(models.Model):
     name = models.CharField(max_length = 100)
     dob = models.DateField('Date Of Birth')
     doj = models.DateField(verbose_name='Date Of Joining')
     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')

Here the dob,doj and proj files will display its name in admin form as per the verbose_name mentioned to those fields.

在这里,dob、doj和proj文件将按照这些字段中提到的verbose_name以管理形式显示其名称。

#4


4  

Building on Javier's answer; if you need one label in forms (on the front-end) and another label on admin it is best to set internal (admin) one in the model and overwrite it on forms. Admin will of course use the label in the model field automatically.

基于哈维尔的回答;如果您需要表单中的一个标签(在前端)和admin中的另一个标签,那么最好在模型中设置一个内部标签(admin)并在表单上覆盖它。管理员当然会自动使用模型字段中的标签。

#5


4  

Meta options

¶元选择

Give your model metadata by using an inner class Meta, like so:

通过使用内部类元数据来给出模型元数据,如下所示:

from django.db import models

class MyClassName(models.Model):

    class Meta:
        verbose_name = "Question"
        verbose_name_plural = "Questions"

human-readable singular and plural names (verbose_name and verbose_name_plural). None are required, and adding class Meta to a model is completely optional.

人可读的单数和复数名称(verbose_name和verbose_name_复)。没有必要,并且将类元添加到模型中是完全可选的。

#1


60  

the verbose name of the field is the (optional) first parameter at field construction.

字段的详细名称是字段构造的第一个参数(可选)。

#2


20  

If your field is a property (a method) then you should use short_description:

如果字段是属性(方法),则应该使用short_description:

class Person(models.Model):
    ...

    def address_report(self, instance):
        ...
    # short_description functions like a model field's verbose_name
    address_report.short_description = "Address"

#3


13  

As Javier suggested you can use verbose name in your fields in model.py. Example as below,

正如Javier所建议的,您可以在model.py的字段中使用详细名称。例子如下,

class Employee(models.Model):
     name = models.CharField(max_length = 100)
     dob = models.DateField('Date Of Birth')
     doj = models.DateField(verbose_name='Date Of Joining')
     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')

Here the dob,doj and proj files will display its name in admin form as per the verbose_name mentioned to those fields.

在这里,dob、doj和proj文件将按照这些字段中提到的verbose_name以管理形式显示其名称。

#4


4  

Building on Javier's answer; if you need one label in forms (on the front-end) and another label on admin it is best to set internal (admin) one in the model and overwrite it on forms. Admin will of course use the label in the model field automatically.

基于哈维尔的回答;如果您需要表单中的一个标签(在前端)和admin中的另一个标签,那么最好在模型中设置一个内部标签(admin)并在表单上覆盖它。管理员当然会自动使用模型字段中的标签。

#5


4  

Meta options

¶元选择

Give your model metadata by using an inner class Meta, like so:

通过使用内部类元数据来给出模型元数据,如下所示:

from django.db import models

class MyClassName(models.Model):

    class Meta:
        verbose_name = "Question"
        verbose_name_plural = "Questions"

human-readable singular and plural names (verbose_name and verbose_name_plural). None are required, and adding class Meta to a model is completely optional.

人可读的单数和复数名称(verbose_name和verbose_name_复)。没有必要,并且将类元添加到模型中是完全可选的。