Andreas
Andreas

Reputation: 636

Setting initial value of Django ModelChoiceField

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?

Upvotes: 2

Views: 9690

Answers (3)

Mateus Padua
Mateus Padua

Reputation: 193

The problem is use request.POST and initial={'name': 'ALASKA'} together. This happens because the values of request.POST always override the values of parameter "initial", so we have to put it separated. My solution was to use this way.

In views.py:

if request.method == "POST":
    form=OtherModelForm(request.POST) 
else:
    form=OtherModelForm() 

In forms.py:

class OtherModelForm(ModelForm):
    name= ModelChoiceField(queryset=OtherModel.objects.all(), initial={'name': 'ALASKA'})

---------- or ----------

In views.py:

if request.method == "POST":
    form=OtherModelForm(request.POST) 
else:
    form=OtherModelForm(initial={'name': 'ALASKA'}) 

In forms.py:

class OtherModelForm(ModelForm):
    name= ModelChoiceField(queryset=OtherModel.objects.all())

Upvotes: 5

Andreas
Andreas

Reputation: 636

I have the follow code:

otherModel = OtherModel.objects.all()
otherModelFormSet = otherModel_formset(id)
otherModelList = otherModel.values('id', 'name')
inProgressOtherModel = otherModelFormSet(None, initial=otherModelList, prefix='otherModel')

Upvotes: 0

Daniel Rosenthal
Daniel Rosenthal

Reputation: 1386

You should have something in your view that looks something along the lines of:

form=OtherModelForm(request.POST, initial={'name': 'ALASKA'}) 

Upvotes: 1

Related Questions