简体   繁体   English

在Django中,作为字段参数的verbose_name和Meta内部class中的verbose_name有什么区别?

[英]In Django, what's the difference between verbose_name as a field parameter, and verbose_name in a Meta inner class?

Consider this class:考虑这个 class:

class Product(models.Model):
  name = models.Charfield(verbose_name="Product Name", max_length=255)

  class Meta:
    verbose_name = "Product Name"

I looked at the Django docs and it says:我查看了 Django 文档,上面写着:

  • For verbose_name in a field declaration: "A human-readable name for the field."对于字段声明中的verbose_name :“字段的人类可读名称。”
  • For verbose_name in a Meta declaration: "A human-readable name for the object, singular".对于Meta声明中的verbose_name :“object 的人类可读名称,单数”。

When would I see either verbose_name manifest at runtime?我什么时候会在运行时看到verbose_name清单? In a form render?在表单渲染中? In Django admin?在 Django 管理员?

The verbose_name in the Meta deals with the name of the model , not field(s) of that model. Meta中的verbose_name处理model 的名称,而不是 model的字段。

It thus likely should be 'Product' , not 'Product Name' :因此它可能应该是'Product' ,而不是'Product Name'

class Product(models.Model):
    name = models.Charfield(verbose_name='Product Name', max_length=255)

    class Meta:
        verbose_name = 'Product'

This thus specifies the table name in the model admin, whereas the verbose_name of the field(s) will show up in ModelForm s and when you display or edit records.因此,这会在 model 管理员中指定表名,而字段的verbose_name将显示在ModelForm中以及显示或编辑记录时。

verbose_name in a field declaration is set when the name of that field (here we mean the name) is not enough for the user to explain what that field is exactly.当该字段的名称(这里我们指的是名称)不足以让用户解释该字段到底是什么时,字段声明中的verbose_name被设置。 For example, maybe you want to provide a more complete description of the name to the user, suppose you mean a short name.例如,也许您想向用户提供更完整的名称描述,假设您的意思是一个短名称。 So it is better to set verbose_name equal to the "short name".所以最好将verbose_name设置为等于“短名称”。 verbose_name in a Meta declaration is set when you want to show your objects individually in a different way to the user in the admin panel.当你想在管理面板中以不同的方式向用户单独显示你的对象时,设置 Meta 声明中的 verbose_name 。 Of course, we also have verbose_name_plural in the meta class. It is used when Django cannot correctly recognize the plural form of a word.当然我们在meta class中还有verbose_name_plural ,当Django无法正确识别单词的复数形式时使用。 Django shows word endings by placing s in plurals, but this is not always true. Django 通过将 s 置于复数形式来显示词尾,但这并不总是正确的。 For example, imagine you have a model called Child.例如,假设您有一个名为 Child 的 model。 Well, now it is better that you set the verbose_name_plural value equal to "children" in the meta class. When you use a language other than English in Django, the above description is more useful.好吧,现在最好在meta class 中将verbose_name_plural值设置为“children”。当您在Django 中使用英语以外的语言时,上面的描述更有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM