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
- [Answered ]-Django form wizard validate (clean) form using previous form
- [Answered ]-How to write the corresponding "handle_uploaded_file" for the model field and form?
- [Answered ]-Find outermost cordinates from Geodjango queryset which has point column
- [Answered ]-OneToOneField is not working django
Source:stackexchange.com