title: How to use Django inlineformset_factory with custom formset
body: <p>I want to use Django inlineformset_factory with custom formset and want to add or remove that inlineformset using <a href="https://github.com/elo80ka/django-dynamic-formset" rel="nofollow noreferrer">django-dynamic-formset</a>.</p>
  
  <p>Form:</p>
  
  <pre><code>class CreatePacketForm(forms.ModelForm):
      """
      CreatePacketForm class
      """
  
      class Meta:
  
          model = Packet
          exclude = ('customer', 'created_on', 'updated_on',
                     'created_by', 'updated_by', 'remark', 'p_id'
                     )
  
  ItemFormSet = inlineformset_factory(Packet, Item, form=CreatePacketForm,
                                      formset=CreateItemForm,
                                      extra=1, max_num=20, can_delete=False)
  </code></pre>
  
  <p>View:</p>
  
  <pre><code>class CreatePacketView(LoginRequiredMixin, CreateView):
      """
      """
  
      model = Packet
      form_class = CreatePacketForm
      template_name = CREATE_PACKET_TEMPLATE
  
      def get(self, request, *args, **kwargs):
              """
              Handles GET requests and instantiates blank versions of the form
              and its inline formsets.
              """
              self.object = None
              form_class = self.get_form_class()
              form = self.get_form(form_class)
              item_form = ItemFormSet()
              return self.render_to_response(self.get_context_data(form=form,
                                             item_form=item_form,))
  </code></pre>
  
  <p>Template:</p>
  
  <pre><code>{% load static %}
  
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;Popup&lt;/title&gt;
  &lt;/head&gt;    
  &lt;body&gt;
  &lt;div style="border: 1px solid;padding: 10px;"&gt;
      &lt;br&gt;
      &lt;div align="middle"&gt;
          &lt;span style="font-size: x-large;"&gt;Please Enter Item Details&lt;/span&gt;
      &lt;/div&gt;
      &lt;br&gt;
      &lt;form  id="packet_form" method="post" action="" style="margin: 0 20% 0 20%;padding: 10px;"&gt;
          {% csrf_token %}
          {{ form.non_field_errors }}
          {{ form }}
          {{ item_form.management_form }}
          {{ item_form.non_form_errors }}
          {% for subform in item_form %}
              &lt;div class="inline {{ item_form.prefix }}"&gt;
                  {{ subform }}
              &lt;/div&gt;
          {% endfor %}
          &lt;div align="middle"&gt;
              &lt;button type="submit"&gt;Submit&lt;/button&gt;
              &lt;button type="reset"&gt;Reset&lt;/button&gt;
          &lt;/div&gt;
      &lt;/form&gt;
  &lt;/div&gt;
  &lt;script type="text/javascript" src="{% static 'girvi/js/jquery-1.3.2.min.js' %}"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="{% static 'girvi/js/jquery.formset.js' %}"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="{% static 'girvi/js/RelatedObjectLookups.js' %}"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="{% static 'girvi/js/swfobject.js' %}"&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;
  $(".inline.{{ item_form.prefix }}").formset({
              prefix: "{{ item_form.prefix }}"
          });
  &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>My inlineformset is working fine without custom formset. But when i render it in my template it does not have <code>management_form</code> as well as all the fields in my formset can be removed.</p>
  
  <p>Rendered template without custom formset</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;&lt;/head&gt;
      &lt;body&gt;
          &lt;div style="border: 1px solid;padding: 10px;"&gt;
              &lt;br&gt;&lt;/br&gt;
              &lt;div align="center"&gt;&lt;/div&gt;
              &lt;br&gt;&lt;/br&gt;
              &lt;form id="packet_form" style="margin: 0 20% 0 20%;padding: 10px;" action="" method="post"&gt;
                  &lt;input type="hidden" value="u3H6thNcyymuwQDrLf5Spnr6E6DEYTxA" name="csrfmiddlewaretoken"&gt;&lt;/input&gt;
                  &lt;input id="id_item_set-TOTAL_FORMS" type="hidden" value="1" name="item_set-TOTAL_FORMS"&gt;&lt;/input&gt;
                  &lt;input id="id_item_set-INITIAL_FORMS" type="hidden" value="0" name="item_set-INITIAL_FORMS"&gt;&lt;/input&gt;
                  &lt;input id="id_item_set-MIN_NUM_FORMS" type="hidden" value="0" name="item_set-MIN_NUM_FORMS"&gt;&lt;/input&gt;
                  &lt;input id="id_item_set-MAX_NUM_FORMS" type="hidden" value="20" name="item_set-MAX_NUM_FORMS"&gt;&lt;/input&gt;
                  &lt;input id="id_item_set-0-id" type="hidden" name="item_set-0-id"&gt;&lt;/input&gt;
                  &lt;div class="inline item_set dynamic-form"&gt;&lt;/div&gt;
                  &lt;a class="add-row" href="javascript:void(0)"&gt;&lt;/a&gt;
                  &lt;div align="center"&gt;&lt;/div&gt;
              &lt;/form&gt;
          &lt;/div&gt;
          &lt;script src="/static/girvi/js/jquery-1.3.2.min.js" type="text/javascript"&gt;&lt;/script&gt;
          &lt;script src="/static/girvi/js/jquery.formset.js" type="text/javascript"&gt;&lt;/script&gt;
          &lt;script src="/static/girvi/js/RelatedObjectLookups.js" type="text/javascript"&gt;&lt;/script&gt;
          &lt;script src="/static/girvi/js/swfobject.js" type="text/javascript"&gt;&lt;/script&gt;
          &lt;script type="text/javascript"&gt;&lt;/script&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>And template with custom formset </p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;&lt;/head&gt;
      &lt;body&gt;
          &lt;div style="border: 1px solid;padding: 10px;"&gt;
              &lt;br&gt;&lt;/br&gt;
              &lt;div align="center"&gt;&lt;/div&gt;
              &lt;br&gt;&lt;/br&gt;
              &lt;form id="packet_form" style="margin: 0 20% 0 20%;padding: 10px;" action="" method="post"&gt;
                  &lt;input type="hidden" value="u3H6thNcyymuwQDrLf5Spnr6E6DEYTxA" name="csrfmiddlewaretoken"&gt;&lt;/input&gt;
                  &lt;div class="inline None dynamic-form"&gt;&lt;/div&gt;
                  &lt;div class="inline None dynamic-form"&gt;&lt;/div&gt;
                  &lt;div class="inline None dynamic-form"&gt;&lt;/div&gt;
                  &lt;div class="inline None dynamic-form"&gt;&lt;/div&gt;
                  &lt;div class="inline None dynamic-form"&gt;&lt;/div&gt;
                  &lt;div class="inline None dynamic-form"&gt;&lt;/div&gt;
                  &lt;div class="inline None dynamic-form"&gt;&lt;/div&gt;
                  &lt;div class="inline None dynamic-form"&gt;&lt;/div&gt;
                  &lt;a class="add-row" href="javascript:void(0)"&gt;&lt;/a&gt;
                  &lt;div align="center"&gt;&lt;/div&gt;
              &lt;/form&gt;
          &lt;/div&gt;
          &lt;script src="/static/girvi/js/jquery-1.3.2.min.js" type="text/javascript"&gt;&lt;/script&gt;
          &lt;script src="/static/girvi/js/jquery.formset.js" type="text/javascript"&gt;&lt;/script&gt;
          &lt;script src="/static/girvi/js/RelatedObjectLookups.js" type="text/javascript"&gt;&lt;/script&gt;
          &lt;script src="/static/girvi/js/swfobject.js" type="text/javascript"&gt;&lt;/script&gt;
          &lt;script type="text/javascript"&gt;&lt;/script&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/XPqEJ.png" alt="without formset"><img src="https://i.stack.imgur.com/ST9cK.png" alt="enter image description here"></p>