I am using inlineformset_factory to generate a formset as so:
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))
I have two questions
Updated with functioning code answering question #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))
would this help? https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#changing-the-queryset
basically, try using queryset kwarg when you create your formset.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With