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']