Reputation: 13
I have the below view for a Formset, but when I save the form it doesn't save changes to the database?
def schedule(request, year, month, day):
EntriesFormset = modelformset_factory(Entry, extra = 1, exclude=("creator", "date"),can_delete=True)
if request.method == 'POST':
formset = EntriesFormset(request.POST)
if formset.is_valid():
# add current user and date to each entry & save
entries = formset.save(commit=False)
for entry in entries:
entry.creator = request.user
entry.date = date(int(year), int(month), int(day))
entry.save()
return HttpResponseRedirect(reverse("Pipettes.views.month", args=(year, month)))
else:
# display formset for existing enties and one extra form
formset = EntriesFormset(queryset=Entry.objects.filter(date__year=year,date__month=month, creator=request.user))
return render_to_response("Scheduler.html", add_csrf(request, entries=formset, year=year,
month=month, day=day))
Upvotes: 1
Views: 1662
Reputation: 45575
I suspect that the formset is invalid but instead of displaying of the formset with errors you returning the redirect. You should move the redirecting to one level right, into the if
statement:
if formset.is_valid():
...
return HttpResponseRedirect(reverse("Pipettes.views.month", args=(year, month)))
UPDATE: If your formset is not validated but you don't see any errors on the page then your formset rendering may be invalid. For testing purposes try to use the simplest possible template:
<table>
{{ formset }}
</table>
Also note that with formset.save(commit=False)
deleted objects are not deleted automatically. See the side note in this chapter of the docs.
Upvotes: 1