[Answered ]-Form index in inlineformset

2👍

No, objects in a collection don’t generally have access to their index or key.

However if you’re outputting the formset in a template, you’re presumably looping through the forms. So you can use {% forloop.counter %} to get the index of the iteration.

0👍

Although it is not pretty, based on the formset source and the comment by @yuji-tomita-tomita above, you could do something like this in your template:

{{ form.prefix|cut:formset.prefix|cut:'-' }}

This just takes the form prefix string, which includes the form index, then removes the irrelevant parts. In the view you could simply do e.g. form.prefix.split('-')[1].

👤djvg

Leave a comment