nolnol
nolnol

Reputation: 21

In Django, in a form, how to dynamically choose custom Model field to be displayed in a ModelChoiceField?

I have to manage records in different languages, with content edited online by registered users, without using Django admin tool.

The forms have to be localized in each language.

As number of languages is fixed, I've decided to handle the different languages with different fields in the model, with the name of the field completed by the language code, cf example below.

# models.py
class MyAttribute(models.Model):
    name_en = models.CharField()     # name in English
    name_es = models.CharField()     # name in Spanish
    name_fr = models.CharField()     # name in French
    field2_en = models.CharField()   # field2 in English
    field2_es = models.CharField()   # field2 in Spanish
    field2_fr = models.CharField()   # field2 in French

class MyGroup(models.Model):
    name_en = models.CharField()     # name in English
    name_es = models.CharField()     # name in Spanish
    name_fr = models.CharField()     # name in French
    myattribute = models.ForeignKey(MyAttribute)

I already created a custom tag to handle display of localized content in my templates and it works well.

{% localized_value mygroup name current_language %}

Now I need to set up a form to edit MyGroup objects, with a models.ModelChoiceField for myattribute field. My understanding is that, by default, this will always display the same string generated by label_from_instance() in the Model, and no way to use request.LANGUAGE_CODE there.

So my question: how to be able to dynamically choose the good name_language_code field to be displayed in my form?

Implementation could be something like:

# views.py    
form.fields["myattribute"].related_field = eval('name_' + language_code)

or

# template.html    
{{ form.myattribute language_code }}

Any Idea?

btw, using Django 1.6

Upvotes: 1

Views: 550

Answers (2)

nolnol
nolnol

Reputation: 21

I finally found a way, using the work described by Beau Simensen : http://srcmvn.com/blog/2013/01/15/django-advanced-model-choice-field/ who modified ModelChoiceField to return (value,label,model) instead of (value,label).

So far, seems to work OK for me.

from django.forms import models
from django.forms.fields import ChoiceField

class AdvancedModelChoiceIterator(models.ModelChoiceIterator):
    def choice(self, obj):
        return (self.field.prepare_value(obj), self.field.label_from_instance(obj), obj)

class AdvancedModelChoiceField(models.ModelChoiceField):
    def _get_choices(self):
        if hasattr(self, '_choices'):
            return self._choices

        return AdvancedModelChoiceIterator(self)

    choices = property(_get_choices, ChoiceField._set_choices)

Upvotes: 1

arannasousa
arannasousa

Reputation: 317

Dude, if you want to make a translation of what is known as verbose_name, ideally u use the django internationalization.

The documentation in the following link: https://docs.djangoproject.com/en/dev/topics/i18n/translation/

Upvotes: 1

Related Questions