So, I'm using Django's Model Formset to produce sets of forms for different data. It is working great, but I want to add a feature where when a user displays the formset and, say, updates 2 out of the 10 items, I can track just the 2 updated, and output a message like "You have updated 2 items" kind-of-thing.
Do Django Model Formsets have a built in API for this? I can't seem to find it on the Django Docs.
I've tried various approaches but keep getting this when using the code offered by Peter below:
'Attendance' object has no attribute 'has_changed.'
If I switch form.has_changed to formset.has_changed(), I get
'list' object has no attribute 'has_changed'
My View and Post method
class AttendanceView(TemplateView):
template_name = 'example.html'
def changed_forms(self, formset):
return sum(1 for form in formset if form.has_changed())
def post(self, request, *args, **kwargs):
formset = AttendanceFormSet(request.POST)
if formset.is_valid():
formset = formset.save()
forms_changed = self.changed_forms(formset)
context = self.get_context_data(**kwargs)
context['total_changed_forms'] = forms_changed
return self.render_to_response(context)
else:
return HttpResponse("POST failed")
So I figured it out, just change:
formset = formset.save()
to
formset.save()