ID of dynamically added formset forms in django templates

539 views Asked by At

This may be a silly question, but:

When rendering forms in a formset in a django template, one has to render the ID's as well. This I get.

When I dynamically create a new form in the formset with JS, I have to again create a hidden ID field. But what value do I give this ID? The template doesn't know what ID's are available. And without a proper ID, the formset wont pass validation back at the server.

Any Ideas?

1

There are 1 answers

2
bdoubleu On

You don't need to give the ID a value, it can just be empty. The new ID is automatically created whenever you call .save().

Trying to anticipate the value of an auto incrementing integer ID isn't ideal, especially with multiple users dynamically adding forms. You wouldn't be able to accurately guess the next ID.