My goal is to create a form that display multiple checkboxes and allows users to select one or more choices. All those choices need to be translated.
While using non-model driven form and CHOICES it works as expected.
COLOR_CHOICES = (
(1, _('Red')),
(2, _('Black')),
)
Since my form is model based instead of MultipleChoiceField I have to use ModelMultipleChoiceField.
colors = forms.ModelMultipleChoiceField(
queryset=Colors.objects, # <- This needs to exist in the database so can't use CHOICES
widget=forms.CheckboxSelectMultiple(),
required=False
)
Since ModelMultipleChoiceField queries the database to get the choices I have to have the data in the database in the first place.
I can think of two solutions to this problems but I am not sure how to make it work (possibly there is a different approach I am not aware of):
-
Create a fixture and translate it (translation is the problem here)
-
Use choices for the
querysetinModelMultipleChoiceField
You should translate
Colorsmodel. I send to you an active project that make easy the job: django-modeltranslationLearn more about this project is in my TODO list.
Also, take a look to Dynamic Translation Apps for Django from muhuk.com blog.