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
- How can i restrict the Model2 objects so that not all applicable objects are present, but only a filtered subset?
- Does anyone have any example code for using class-based views with formsets?
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))