2

I have the below model that includes a field called boxnumber When i don't use DAL, the verbose_name and help_text appear and get translated when needed.

But when adding DAL (see modelform below), it only shows the name, not translated and with no help text.

Any suggestions?

control/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

Info: DAL 3.1.8 Django 1.10.1 Python 3.4

Yarh
  • 895
  • 7
  • 15

3 Answers3

3

You replace the 'default' widget with a dal widget , then you have to add 'refresh' it like this

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

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

wllbll
  • 531
  • 5
  • 11
2

For me, verbose_name and helptext were lost using ChoiceField.

But ChoiceField is not a widget, it is form field something. Putting it into Meta as a widget raises an error.

Retyping the verbose_name and help_text is definitely not DRY.

This worked for me:

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 )

(My model is named Search.)

More DRY!

Rick Graves
  • 517
  • 5
  • 11
0

It's not specific to dal. You're re-instanciating a new widget class, so you need to copy help_text and verbose_name yourself.

jpic
  • 32,891
  • 5
  • 112
  • 113