使用django自动完成灯时,Verbose_name和helptext丢失

问题描述 投票:0回答:3

我有下面的模型,其中包含一个名为boxnumber的字段当我不使用DAL时,verbose_name和help_text会出现并在需要时进行翻译。

但是当添加DAL时(参见下面的模型),它只显示名称,而不是翻译,没有帮助文本。

有什么建议?

控制/ models.py:

from django.utils.translation import ugettext_lazy as _

class Command(models.Model):
    ....
    boxnumber = models.ForeignKey(SmartBox, models.SET_NULL, blank=True, null=True,
                                  help_text=_("the Smart Box # on this client"),
                                  verbose_name=_('Box-Number')
                                  )

class CommandForm(ModelForm):
    class Meta:
        model = Command
        fields = [...,
                  'boxnumber',
                  ... ]


    boxnumber = forms.ModelChoiceField(
        queryset=SmartBox.objects.all(),
        widget=autocomplete.ModelSelect2(url='control/boxnumber-autocomplete',
                                         forward=['group'])
    )   # adding this removes help_text and verbose_name

信息:DAL 3.1.8 Django 1.10.1 Python 3.4

python django django-autocomplete-light
3个回答
2
投票

对我来说,使用ChoiceField丢失了verbose_name和helptext。

但ChoiceField不是一个小部件,它是表单领域的东西。将它作为小部件放入Meta会引发错误。

重新编写verbose_name和help_text绝对不是DRY。

这对我有用:

class SearchAddOrUpdateForm(ModelForm):
    priority = forms.ChoiceField(
        choices     = ALL_PRIORITIES,
        label       = Search._meta.get_field('priority').verbose_name,
        help_text   = Search._meta.get_field('priority').help_text )

(我的模型名为Search。)

更干!


0
投票

这不是dal特有的。您正在重新实例化一个新的窗口小部件类,因此您需要自己复制help_text和verbose_name。


0
投票

您使用dal小部件替换“默认”小部件,然后您必须像这样添加“刷新”

class CommandForm(ModelForm):
class Meta:
    model = Command
    fields = [...,
              'boxnumber',
              ... ]


boxnumber = forms.ModelChoiceField(
    queryset=SmartBox.objects.all(),
    widget=autocomplete.ModelSelect2(
               url='control/boxnumber-autocomplete',
               forward=['group']
    )
    label=_('Box-Number')
    help_text=_("the Smart Box # on this client")
)   # adding this removes help_text and verbose_name

提到:https://docs.djangoproject.com/en/1.11/ref/forms/fields/#label https://docs.djangoproject.com/en/1.11/ref/forms/fields/#help-text

© www.soinside.com 2019 - 2024. All rights reserved.