I have been trying to do Django class-based CreateView and UpdateView with multiple inline formsets
CreateView works fine but UpdateView is not working properly, If
I'm not sure if you've found an answer, but I have a working version of UpdateView documented in my answer found here:
UpdateView with inline formsets trying to save duplicate records?
I don't think that the regular form of the updateview has to be added to the context because it is there anyways. A working Updateview with inlineformsets could be achieved less complicated. I based this on this Question
class RecipeUpdateView(UpdateView):
model = Recipe
form_class = RecipeUpdateForm
success_url = "/foo/"
def get_success_url(self):
self.success_url = '/account/dashboard/'
return self.success_url
def get_object(self):
return #your object
def get_context_data(self, **kwargs):
context = super(RecipeUpdateView, self).get_context_data(**kwargs)
if self.request.POST:
context['ingredient_form'] = IngredientFormSet(self.request.POST, instance=self.object)
context['instruction_form'] = InstructionFormSet(self.request.POST, instance=self.object)
else:
context['ingredient_form'] = IngredientFormSet(instance=self.object)
context['instruction_form'] = InstructionFormSet(instance=self.object)
return context
def form_valid(self, form):
context = self.get_context_data()
ingredient_form = context['ingredient_form']
instruction_form = context['instruction_form']
if ingredient_form.is_valid() and instruction_form.is_valid():
self.object = form.save()
ingredient_form.instance = self.object
ingredient_form.save()
instruction_form.instance = self.object
instruction_form.save()
return self.render_to_response(self.get_context_data(form=form))
My guess is that you can't do
self.object = None
on overwritten post
method in a UpdateView
. So, try
self.object = self.get_object()
instead, once you already have an object instance in this case.
So I recognize the models form this post. To get UpdateView working properly you are going to have to do at least two, maybe three things:
Update the self.object = self.get_object()
-- after that, your ability to dynamically add should work.
To get the dynamic deletes updating properly, you will need to alter the template with form.DELETE (in two places, the ingredients and the instructions).
{{ form.description }}
{% if form.instance.pk %}{{ form.DELETE }}{% endif %}
Not sure it was necessary but I added can_delete to the factory too.
IngredientFormSet = inlineformset_factory(Recipe, Ingredient, fields=('description',), extra=3, can_delete=True)
InstructionFormSet = inlineformset_factory(Recipe, Instruction, fields=('number', 'description',), extra=1, can_delete=True)