Setting initial value of Django ModelChoiceField

◇◆丶佛笑我妖孽 提交于 2019-12-21 22:27:57

问题


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?


回答1:


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())



回答2:


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

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



回答3:


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


来源:https://stackoverflow.com/questions/17178573/setting-initial-value-of-django-modelchoicefield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!