How to change the prefix of a formeset's empty_form?

32 views
Skip to first unread message

Hércules Lopes

unread,
Oct 19, 2022, 1:33:58 PM10/19/22
to Django users
I'm working on a page with nested formsets, it works fine with static inline formsets, but when I try to create forms dynamically I get a problem. The prefix generated for a nested formset is its parent's one + its own, like: student-0-address-0.

But for empty_form both are just '__prefix__, like': student-__prefix__-address-__prefix__.

Eventually when the user adds a new form my javascript will change every '__prefix__' in the new cloned empty_form element to the amount of class="student-form" there is, so it ends up like this:

# first nested formset generated:
<label for="id_student-0-address-0-street">Street:</label>
<input id="id_student-0-address-0-street " ...>

# second nested formset generated:
<label for="id_student-1-address-1-street">Street:</label>
<input id="id_student-1-address-1-street " ...>

The second -1- here should be zero, but my regex changes every '__prefix__'. I suppose I could change the regex to only target each individually, but then I'd have two problems, so how can i change the default empty_form prefix?
Reply all
Reply to author
Forward
0 new messages