[Django]-Choices in Django model not being translated, possibly due to use of modelform or modelformset?

0👍

I found a way to make it work. To be quite truthful, I don’t understand every aspect of what’s going on here, but I puzzled it together from related issues.

The workaround seems to be to create an explicit modelform and to use a lazy version of ChoiceField. I found code for the latter here. Here’s my forms.py:

class LazyChoiceField(ChoiceField):
    '''
    A Lazy ChoiceField.
    This ChoiceField does not unwind choices until a deepcopy is called on it.
    This allows for dynamic choices generation every time an instance of a Form is created.
    '''
    def __init__(self, *args, **kwargs):
        # remove choices from kwargs.
        # choices should be an iterable
        self._lazy_choices = kwargs.pop('choices',())
        super(LazyChoiceField,self).__init__(*args, **kwargs)

    def __deepcopy__(self, memo):
        result = super(LazyChoiceField,self).__deepcopy__(memo)
        lz = self._lazy_choices
        if callable(lz):
            lz = lz()
        result.choices = lz
        return result

class IssueForm(forms.ModelForm):

    who_can_help = LazyChoiceField(choices=((PEOPLE, _('People')),
                                            (OWNERS, _('Owners')),
                                            (EXPERIENCED_PEOPLE, _('People with experience'))))

    class Meta:
        model = Issue
        fields = ['title', 'description', 'plan']

I got the idea from this similar issue.

It’s not that I don’t have any clue why this works, but it’s not as sharp in my mind as it could be yet, so if anyone has any further insights about why this is needed and why it works, they are still welcome.

0👍

I had this problem and ended up using simple tuples with text
enter image description here

Leave a comment