[Answered ]-Inlineformset_factory custom form id

2👍

What I did was create an extra form that is hidden, and then simply copy it with Javascript.
That is copy the last form, make the hidden form Visible, and hide the new copy of the form.

This way you can just Count the number of forms on the page and use that for you meta Total-Forms variables.

Some fussing will need to be done with DELETED attributes if you want to be able to delete forms on the page.
Edit: Here is one of my main functions using prototype js.

function add_form_to_formset(formset, callbacks etc...){
   // Management form
   total_forms_element = $('id_' + formset.prefix + '-TOTAL_FORMS');

   var curr_last_id = biggest_id(formset.form_class);
   var last_form = $('id_' + formset.prefix + '-' + curr_last_id + '-id').up();

   // Copy it
   var new_form = last_form.cloneNode(true);

   ...register some buttons like add/remove etc..

   set_form_id(new_form, curr_last_id+1);

   last_form.show();
   last_form.insert({after:new_form});


   // Increment the management form count
   total_forms_element.value = parseInt(total_forms_element.value) + 1;

0👍

Instead of generating formset factory, you can use auto_id parameter to generate a form with unique ids:

def get_form(request, form_type):
    current_forms_no = int(request.GET["current_forms_no"])
    form_class = all_forms[form_type]
    form = form_class(auto_id='id_%%s_%s' % current_forms_no)
    return HttpResponse(form.as_p())

Leave a comment