我正在使用inlineformset_factory来生成一个formset:
FormSet = inlineformset_factory(Model1, Model2, extra=0)
if request.method =="POST":
formset = FormSet(request.POST, instance=model1_object)
if formset.is_valid():
formset.save()
return HttpResponseRedirect( reverse-blah )
else:
formset = PupsFormSet(instance=model1_object,)
return render_to_response("template.html", {"formset":formset, 'model1_object':model1_object},context_instance=RequestContext(request))
我有两个问题
更新了功能代码回答问题#1
FormSet = inlineformset_factory(Model1, Model2, extra=0)
if request.method =="POST":
formset = FormSet(request.POST, instance=model1_object, queryset=Model2.objects.filter(foo=bar))
if formset.is_valid():
formset.save()
return HttpResponseRedirect( reverse-blah )
else:
formset = PupsFormSet(instance=model1_object, queryset=Model2.objects.filter(foo=bar))
return render_to_response("template.html", {"formset":formset, 'model1_object':model1_object},context_instance=RequestContext(request))
答案 0 :(得分:7)
基本上,在创建formset时尝试使用queryset kwarg。