如何在 forms.py Django 中添加 verbose_name?

How to add a verbose_name in forms.py Django?

class ApplicationForm(BaseForm):
    class Meta:
        model = Application
        fields = ['referencenumber', 'name', 'description', 'owner']

我有来自 models.py 的上述表格。但是我想在表单上放置不同于 models.py 的 verbose_name 的标签。我无法编辑 models.py,因为我们的开发太过火了。

有什么办法可以在表格中做到这一点?请帮忙!

标签是 verbose_names 的模型,您可以更改它。这将帮助您:

解决方案 #1

class ApplicationForm(BaseForm):
    class Meta:
        model = Application
        fields = ['referencenumber', 'name', 'description', 'owner']

    def __init__(self, *args, **kwargs):
        super(ApplicationForm, self).__init__(*args, **kwargs)
        self.fields['referencenumber'].label = "reference number"
        self.fields['name'].label = "name"

解决方案 #2

class ApplicationForm(BaseForm):
    class Meta:
        model = Application
        fields = ['referencenumber', 'name', 'description', 'owner']
        labels = {
            'referencenumber': 'referencenumber',
            'name': 'name',
        }