Django class-based CreateView and UpdateView with multiple inline formsets

前端 未结 4 1587
抹茶落季
抹茶落季 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: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)
      

提交回复
热议问题