Django Formset - Access the primary key id input
-
02-07-2021 - |
Question
I want to render primary key id hidden input (<input type="hidden" name="form-0-id" value="5" id="id_form-0-id">
) directly in my template. I know I can render whole form, but I don't want to have any labels there. Is there some way to get it using the formset? I used object initial.id
, but it didn't work.
Method:
def getCellEditForm(self):
CellFormSet = modelformset_factory(Cell, extra=0, max_num=0)
form = CellFormSet(queryset=Cell.objects.filter(pk=self.id))
return form
Template:
{{ child.getCellEditForm.form.title }}
{{ child.getCellEditForm.form.parent }}
{{ child.getCellEditForm.form.initial.id }}
Solution
{% for subform in formset.forms %}
{{ subform.id }}
{% endfor %}
or
{% for subform in formset.forms %}
<input type='hidden' id='id_form{{subform.id}}' name='form{{subform.id}}' value='{{subform.id}}' />
{% endfor %}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow