此问题与this有关。
我打算从表单字段中保存值。下面是我的循环代码代码:
def form_valid(self, form):
tooth = [18, 17, 16, 15, 14, 13, 12, 11, 21, 22, 23, 24, 25, 26, 27, 28, 48, 47, 46, 45, 44, 43, 42, 41, 31, 32, 33, 34, 35, 36, 37, 38]
tooth.reverse()
mytooth = {}
pt = self.kwargs['pk']
for t in tooth:
mytooth = self.request.POST.getlist('tooth_' + str(t), [])
mytooth = " ".join(mytooth)
form.instance.status = mytooth
form.instance.position = t
form.instance.patient = PatientInfo.objects.get(pk=pt)
return super(DentalRecordCreateView, self).form_valid(form)
问题在于我可以使用
查看值show = str(t) + mytooth
messages.success(self.request, show) # display values
但只有字段form.instance.patient
在我的数据库中有值。我在这里缺少什么?
答案 0 :(得分:0)
我认为您错过了for循环中的form.save()
。这至少可以解释为什么数据库中没有数据。
它应该看起来像:
def form_valid(self, form):
tooth = [...]
tooth.reverse()
mytooth = {}
pt = self.kwargs['pk']
for t in tooth:
mytooth = self.request.POST.getlist('tooth_' + str(t), [])
mytooth = " ".join(mytooth)
form.instance.status = me
form.instance.position = t
form.instance.patient = PatientInfo.objects.get(pk=pt)
form.save()
return super(DentalRecordCreateView, self).form_valid(form)