所以,我有一个SelectWithPop表单。因此用户可以选择一个变量,或者他可以再添加一个变量。 将打开一个新页面供用户输入数据。
我的问题是我能够在模型中插入一个新变量,但当我返回页面选择一个值时,直到我重新启动服务器才会显示新值。
这种行为是正常的还是我做错了什么?
如果是,有没有办法克服这个问题?
以下是我的观点:
def add(request, field):
return handlePopAdd(request, HospitalForm, 'hospital_name')
def handlePopAdd(request, addForm, field):
if request.method == "POST":
form = addForm(request.POST)
if form.is_valid():
try:
newObject = form.save()
except forms.ValidationError, error:
newObject = None
if newObject:
return HttpResponseRedirect(reverse('form', args=['clinical']))
else:
form = addForm()
return render_to_response("popadd.html", { 'form': form })
形式:
class CheckPatForm(forms.Form):
pat = forms.IntegerField(label="Paciente")
_names = list(Hospital.objects.values_list('hospital_id', 'hospital_name'))
_names.append(('',''))
hosp = forms.ChoiceField(_names, widget=SelectWithPop(), label="Hospital", required=False)
proc = forms.IntegerField(label="Processo", required=False)
class HospitalForm(forms.ModelForm):
class Meta:
model = Hospital
模板添加:
<a
href="/SIAM-TB/insert/form/add/{{ field }}"
class="add-another"
id="add_id_{{ field }}">
<img src="http://rome/SIAM-TB/admin_media/img/admin/icon_addlink.gif"
width="10" height="10" alt="Add Another"/>
</a>
答案 0 :(得分:0)
将获取医院查询集的代码放入表单init中。例如
....
hosp = forms.ChoiceField(widget=SelectWithPop(), label="Hospital", required=False)
def __init__(self, *args, **kwargs):
super(CheckPatForm, self).__init__(*args, **kwargs)
names = list(Hospital.objects.values_list('hospital_id', 'hospital_name'))
names.append(('',''))
self.fields['hosp'].choices = names