0

I have a form from a model. Depending on "property_type" would be dynamically build a (or multiple) different field for "property_values". The result HTML form is right but the options for the multiple select field for "Property value" will not be selected from the instance.

My form is:

class MyForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        super(MyForm, self).__init__(*args, **kwargs)

        if 'instance' in kwargs:
            if self.instance.property_type == models.Property.RANGE:
                self.fields['property_values'] = forms.MultipleChoiceField(
                    choices=(
                        ('lt', '<'),
                        ('eq', '='),
                        ('gt', '>')
                    )
                )
            self.fields['property_values'].label = 'Value'

    class Meta:
        model = models.Property
        fields = ['property_type', 'property_values']
        labels = {
            'property_type': 'Type',
        }
        widgets = {
            'property_type': forms.Select(),
        }

In the Database the field property_values is: ['eq', 'gt']

enter image description here

Antonio Romero Oca
  • 1,196
  • 10
  • 31
  • You do set these three choices (options) in the constructor of your modelForm - and they are displayed. So what's the issue exactly? I didn't get it. – Max M Aug 11 '17 at 08:34
  • The select structure ist right. But the problem is that the values from the instance will not be selected at that select. There is no option:selected – Antonio Romero Oca Aug 11 '17 at 08:37
  • Try again after adding a widget to your field: `self.fields['property_values'] = forms.MultipleChoiceField( choices=( ('lt', '<'), ('eq', '='), ('gt', '>') ), widget=forms.SelectMultiple())` – Max M Aug 11 '17 at 08:41
  • It does not work. At the MultipleChoiceField class definition is alredy `widget = SelectMultiple` – Antonio Romero Oca Aug 11 '17 at 08:53
  • Mhhh... another idea is to initialize the choices in your Meta class. You can declare the `forms.SelectMultiple()` widget there (though it is the default widget) and add choices to the attrs of that widget. `widgets = { 'property_type': forms.Select(), 'property_values' : forms.SelectMultiple(attrs=('choices' : ( ('lt', '<'), ('eq', '='), ('gt', '>') ) )) }` – Max M Aug 11 '17 at 09:00
  • The question is that the field "property_values" must be dynamically set depending on "property_type". For each type may be a different type of field. – Antonio Romero Oca Aug 11 '17 at 09:08
  • But in your code, property_values has these 3 choices fixed. Is your issue that (1) these 3 options are always there (static), though they're meant to be dynamic. Or (2) the selected items of property_values are not recognized as selected options in your http-POST? – Max M Aug 11 '17 at 09:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/151697/discussion-between-antonioromero-and-max-m). – Antonio Romero Oca Aug 11 '17 at 09:21

0 Answers0