这可能是一个相当简单的问题。但我试图用从数据库中获取的初始数据填充我的表单。我真的不知道从哪里开始。任何建议都会有帮助。
一些背景:我患有多种指标的疾病。在我的表单中,我提取了disease_id = 2,然后想要填充数据库中当前的指标,因此可以对它们进行编辑。
views.py
def drui(request):
disease_id = request.GET.get('disease_id', '2')
if request.method == "POST":
disease = get_object_or_404(Disease, pk=disease_id)
diseaseForm = DiseaseForm(request.POST, instance=disease)
if diseaseForm.is_valid():
new_disease = diseaseForm.save(commit=False)
indicatorInlineFormSet = IndicatorFormSet(request.POST, request.FILES, instance=new_disease)
if indicatorInlineFormSet.is_valid():
new_disease.save()
indicatorInlineFormSet.save()
return HttpResponseRedirect(reverse(somewhere))
else:
diseaseForm = DiseaseForm()
indicatorInlineFormSet = IndicatorFormSet(instance=Disease())
return render_to_response("drui.html", {'diseaseForm': diseaseForm, 'indicatorInlineFormSet': indicatorInlineFormSet},context_instance=RequestContext(request))
答案 0 :(得分:1)
将疾病对象放在请求POST
中def drui(request):
disease_id = request.GET.get('disease_id', '2')
disease = get_object_or_404(Disease, pk=disease_id)
if request.method == "POST":
diseaseForm = DiseaseForm(request.POST, instance=disease)
if diseaseForm.is_valid():
new_disease = diseaseForm.save(commit=False)
indicatorInlineFormSet = IndicatorFormSet(request.POST, request.FILES,
instance=new_disease)
if indicatorInlineFormSet.is_valid():
new_disease.save()
indicatorInlineFormSet.save()
return HttpResponseRedirect(reverse(somewhere))
else:
diseaseForm = DiseaseForm(instance=disease)
indicatorInlineFormSet = IndicatorFormSet(instance=disease)
return render_to_response("drui.html", {
'diseaseForm': diseaseForm,
'indicatorInlineFormSet': indicatorInlineFormSet
},context_instance=RequestContext(request))