设置Django ModelChoiceField的初始值 [英] Setting initial value of Django ModelChoiceField
问题描述
class OtherModel(models.Model)
name = models.CharField(max_length = 100,null = True)
类模型(models.Model)
name = models.CharField(max_length = 100,null = True)
otherModel = models.ManyToManyField(OtherModel)
对于类Model,我使用一个普通的FormSet() 。对于类otherModel我使用formset_factory()
我只想允许从OtherModel从数据库中选择数据,所以我将OtherModel中的CharField名称更改为ModelChoiceField,这个代码:
def otherModel_formset(self,patientenID):
class OtherModelForm(ModelForm) b $ b 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)
我可以将所选的名称保存在m2m字段中重新加载他们没有选择
例如:
< select id = some_idname =some_name>
< option value =1> HAWAII< / option&g t;
< option value =2> ALASKA< /选项>
< / select>
在提交和重新加载中选择ALASKA,应该看起来像这样: / p>
< select id = some_idname =some_name>
< option value =1> ; HAWAII< / option>
< option value =2** selected =selected**> ALASKA< / option>
< / select>
但这个站在html内容中:
code>< select id = some_idname =some_name>
< option value =1> HAWAII< / option>
< option value =2> ALASKA< /选项>
< / select>
有人知道一个解决方案?
问题是使用 request.POST
和 initial = {'name':'ALASKA'}
在一起这是因为 request.POST
的值总是覆盖参数initial的值,所以我们必须将其分开。我的解决方案是使用这种方式。
在 views.py
中:
if request.method ==POST:
form = OtherModelForm(request.POST)
else:
form = OtherModelForm ()
在 forms.py
:
class OtherModelForm(ModelForm):
name = ModelChoiceField(queryset = OtherModel.objects.all(),initial = 'name':'ALASKA'})
----------或 - ---------
在 views.py
中:
if request.method ==POST:
form = OtherModelForm(request.POST)
else:
form = OtherModelForm(initial = {'name':'ALASKA'})
在 form.py
:
class OtherModelForm(ModelForm):
name = ModelChoiceField(queryset = OtherModel.objects.all())
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?
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())
这篇关于设置Django ModelChoiceField的初始值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!