Django class-based CreateView and UpdateView with multiple inline formsets

前端 未结 4 1586
抹茶落季
抹茶落季 2020-12-23 10:52

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

相关标签:
4条回答
  • 2020-12-23 11:04

    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?

    0 讨论(0)
  • 2020-12-23 11:06

    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))
    
    0 讨论(0)
  • 2020-12-23 11:07

    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.

    0 讨论(0)
  • 2020-12-23 11:23

    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:

    1. Update the self.object = self.get_object() -- after that, your ability to dynamically add should work.

    2. 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 %}
      
    3. 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)
      
    0 讨论(0)
提交回复
热议问题