8π
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))
7π
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.
- [Django]-Name '_' is not defined
- [Django]-Celery: When should you choose Redis as a message broker over RabbitMQ?
- [Django]-Django + apache & mod_wsgi: having to restart apache after changes
4π
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?
- [Django]-How to correct this error: "'Adminsite' object has no attribute 'root'"
- [Django]-Install mysql-python (Windows)
- [Django]-How can reference the last item in a list in a Django template? {{ list.-1.key }}
0π
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)
- [Django]-How to correct this error: "'Adminsite' object has no attribute 'root'"
- [Django]-POST jQuery array to Django
- [Django]-Django Sitemaps and "normal" views