Django model formset, "(Hidden field id) This field is required."
I'm trying to use a modelformset
to update existing objcts, but cannot get the form to submit because (i'm assuming) the id associated with each object is not being passed by the form.I see "(Hidden field id) This field is required." in the template.
My code models.py:
class FoodItem(models.Model):
name = models.CharField(max_length = 100)
views.py:
def edit(request):
FormSet = modelformset_factory(FoodItem, include = ('name',))
if request.method == 'POST':
formset = FormSet(request.POST)
if formset.is_valid():
formset.save()
else:
formset = FormSet(queryset=FoodItem.objects.all())
return render(request, 'core/edit.html', {'formset': formset})
and the template:
<form class="" action="." method="post" enctype="multipart/form-data">
{{ formset.management_data }}
{% csrf_token %}
{{ formset.as_p }}
<input type="submit" name="" value="Update">
</form>
I have aslo tried rendering each one individually and including hidden_fields
:
<form class="" action="." method="post" enctype="multipart/form-data">
{{ formset.management_data }}
{% csrf_token %}
{% for form in formset %}
{{form.as_p}}
{{form.hidden_fields}}
{% endfor %}
<input type="submit" name="" value="Update">
</form>
but this didn't work. Thanks for any help with this.
Changed
FormSet = modelformset_factory(FoodItem, include = ('name',))
to
FormSet = modelformset_factory(FoodItem, include = '__all__')
and it cleared it up, guess if you are passing a tuple to the include parameter you have to manually include the id field.