[Answered ]-Why does cloning the django inline formsets result to forms with similar ids and names?

1πŸ‘

βœ…

I use the CloneMore.js script for the same purpose (my version is modifed at this point). I don’t know if this is the case for you, but if you have any field that is being modified by javascript, you need to reinitialize that field. For example, uniform.js selectors, datepickers, or any autocomplete field may need to be reset.

Here is a snippet of my code for dealing with some of these issues when I hit the clone button. Also notice that these are within live()

// #add_more is my button to add a formset
$('#add_more').live('click',function() { 
    cloneMore('div.line_item:last', 'lineitem');
    $(".line_item:last").find('.hasDatepicker').each(function() {
        // remove then initialize datepicker
        $(this).removeClass("hasDatepicker").datepicker( {dateFormat:'mm/dd/yy'} );
    });
    $(".line_item:last").find('.autocomplete_field').each(function() {
        // remove then initialize autocomplete
        $(this).autocomplete('destroy');
        // enable_autocomplete is a custom function, but it's basically just 
        // $(this).autocomplete({ options... })
        enable_autocomplete($(this), cust_part_url);
    });
});

Edit: I think this SO Answer is the origin of the cloneMore script

πŸ‘€j_syk

1πŸ‘

Each form in a django formset is supplied with unique name attribute which acts as a sort of id to django, which means if you clone a first form in a formset it means you will also clone its ids and hence the clone it will override the first form.

You can use/clone empty_form supplied by Baseformset, {{form.empty_form}}, supply it with appropriate names and update the -TOTAL_FORMS

πŸ‘€machaku

Leave a comment