R
R
Roman Mindlin2017-04-24 18:32:28
Django
Roman Mindlin, 2017-04-24 18:32:28

How to pass a localized verbose_name from the model to the form?

I have a form with this field:

year_first = forms.DateField(label=Clone._meta.get_field('year_first').verbose_name.capitalize(),
                                input_formats=['%y', '%Y', '%d.%m.%Y', '%d-%m-%Y'], required=False,
                                widget = YearTextInput)

those. this is a DateField in which you can simply enter a year and it will then be converted to some date.
I can't understand why the label always takes the localized name from the model?
In the model, the field is done like this:
year_first = models.DateField(db_index=True, blank=True, null=True, verbose_name=_('year released'))

So on the form, the signature for this field is always obtained in Russian, while with the rest of the fields (which are created automatically via class Meta), everything is fine. The fields on the template are displayed in a cycle, everything is the same.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Mindlin, 2017-04-25
@kgbplus

In general, if anyone is interested, it seems to be a Django bug described here
. In short, the capitalize () method is to blame, it is not supported by lazy objects. One solution, described on the page with a bug, another (similar) I found myself:
In the form class:

@lazy
def capit(string):
   return string.capitalize()

year_first = forms.DateField(label=capit(Clone._meta.get_field('year_first').verbose_name),
                            input_formats=['%y', '%Y', '%d.%m.%Y', '%d-%m-%Y'], required=False,
                            widget = YearTextInput)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question