Why does django-dynamic-formset 'add another' button duplicate foreign key field?

Viewed 355

I went through the instructions to set up django-dynamic-formset to my project and my specific inline formsets.

{{ Edit }}

The goal is to provide an easy way to add and remove individual forms within an formset.

{{ End edit }}

All of the formsets seem to work except one that includes a foreign key. When I 'add another' it duplicates that field right under it.

Before pressing 'add another': enter image description here

After pressing 'add another': enter image description here

The formset:

<h3>Services</h3>
<div class="row">  
    {{ services_formset.management_form }}
    {% for formserve in services_formset %}
    {{ formserve.non_field_errors }}
    <div class="container" id="services_formset">
        <div class="row" name="service_form">    
            {% for hidden in formserve.hidden_fields %}
                {{ hidden }}
            {% endfor %}                                         
            {% for field in formserve %}
                {% if field.name != 'index' and field.name != 'invoice'%}
                    <div class="col-sm">                     
                        {{ field.errors }}
                        {{ field|as_crispy_field }}
                        {% if field.help_text %}
                        <p class="help">{{ field.help_text|safe }}</p>
                        {% endif %}
                    </div>
                {% endif %}
            {% endfor %}                
        </div>
     </div> 

The javascript:

<script type="text/javascript">
    $(function() {
        $('#services_formset').formset({
            prefix: '{{ formserve.prefix }}'
        });
    })

</script>

EDIT: I use inlineformset_factory to create it:

ServicesFormset = inlineformset_factory(TransactionsTable, TransSpTable, fields=('service','fee','qty','taxrate','tax','sub'), extra=1, widgets=services_widgets, can_delete=True)

I then instantiate it using a function that checks if it's a create or detail view and a POST or not:

context['services_formset'] = get_dynamic_formset(self, ServicesFormset)

def get_dynamic_formset(view, FormSetClass):
    if view.object:
        if view.request.POST:
            dynamic_formset = FormSetClass(view.request.POST, instance=view.object)
        else:
            dynamic_formset = FormSetClass(instance=view.object)
    else:
        if view.request.POST:
            dynamic_formset = FormSetClass(view.request.POST)
        else:
            dynamic_formset = FormSetClass()   
    return dynamic_formset
0 Answers
Related