I have an Model with an m2m-Fields to an OtherModel.
class OtherModel(models.Model)
name = models.CharField(max_length=100, null=True)
class Model(models.Model)
name = models.CharField(max_length=100, null=True)
otherModel = models.ManyToManyField(OtherModel)
For the class Model I use an normal FormSet(). For the class otherModel I use a formset_factory()
I only want to allowed to select data from the database from OtherModel so I changed the CharField name in OtherModel to a ModelChoiceField with this code:
def otherModel_formset(self, patientenID):
class OtherModelForm(ModelForm):
name= ModelChoiceField(queryset=OtherModel.objects.all())
def __init__(self, *args, **kwargs):
super(OtherModelForm, self).__init__(*args, **kwargs)
class Meta:
model = OtherModel
fields = ['name']
return formset_factory(form=OtherModelForm, max_num=10)
I can save the selected name in the m2m Field but on the reload they selected nothing
exampel:
<select id=some_id" name="some_name">
<option value="1"> HAWAII </option>
<option value="2"> ALASKA</option>
</select>
In the exampel ALASKA is selected on submit and on reload that should look in a kind of this:
<select id=some_id" name="some_name">
<option value="1"> HAWAII </option>
<option value="2" **selected="selected"**> ALASKA</option>
</select>
but this stand in the html content:
<select id=some_id" name="some_name">
<option value="1"> HAWAII </option>
<option value="2"> ALASKA</option>
</select>
Somebody know a solution?