So, I've a form with SelectWithPop. So the user can select a variable or he can add one more. A new page is open for the user to enter data.
My problem is that I'm able to insert a new variable into the model but when I return to the page to select a value, the new value is not shown until I restart the server.
Is this behavior normal or I've done something wrong? If it is, is there any way to overcome this issue?Here are my views:
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 })
forms:
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
templates add:
<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>
put the code that gets the hospital queryset into the forms init. e.g.
....
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
精彩评论