Model formset not saving

340 views
Skip to first unread message

cbpa...@gmail.com

unread,
Jul 16, 2018, 6:06:00 PM7/16/18
to Django users
I'm using Django 2.0.3 on Ubuntu 16.04. Any idea what would cause a model formset to return None when calling save()? Here is the relevant code:

RepairFormSet = modelformset_factory(Repair, form=RepairForm, exclude=[])

form
= RepairFormSet(request.POST)

if form.is_valid():
  form
.save()
 
return render(request, 'message-loggedin', {
   
'title': 'Success',
   
'message': 'A repair request has been made successfully.'
 
})
else:
 
return render(request, 'message-loggedin', {
   
'title': 'Error',
   
'message': format_html(str(form.errors))
 
})

I checked the database and nothing is added to it, form.save() just always returns None. When I try to debug within django/forms/models.py it seems that form.has_changed() always returns False, causing nothing to be saved. However I do not understand why it thinks nothing has been changed. Any help would be greatly appreciated.

cbpa...@gmail.com

unread,
Jul 17, 2018, 3:57:21 PM7/17/18
to Django users
Is there a paid support option to get help? I've been trying to ask for help on IRC for a while but all I've gotten is insults.

Glen D souza

unread,
Jul 18, 2018, 12:40:06 AM7/18/18
to django...@googlegroups.com
have you tried 
form.save(commit = True) ?


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/00a3c7fa-820f-4f4c-8734-07d2c542d596%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

cbpa...@gmail.com

unread,
Jul 18, 2018, 1:13:50 AM7/18/18
to Django users
Yes, it is still returning None.


On Wednesday, July 18, 2018 at 12:40:06 AM UTC-4, Glen D souza wrote:
have you tried 
form.save(commit = True) ?

On 18 July 2018 at 01:27, <cbpa...@gmail.com> wrote:
Is there a paid support option to get help? I've been trying to ask for help on IRC for a while but all I've gotten is insults.

On Monday, July 16, 2018 at 6:06:00 PM UTC-4, cbpa...@gmail.com wrote:
I'm using Django 2.0.3 on Ubuntu 16.04. Any idea what would cause a model formset to return None when calling save()? Here is the relevant code:

RepairFormSet = modelformset_factory(Repair, form=RepairForm, exclude=[])

form
= RepairFormSet(request.POST)

if form.is_valid():
  form
.save()
 
return render(request, 'message-loggedin', {
   
'title': 'Success',
   
'message': 'A repair request has been made successfully.'
 
})
else:
 
return render(request, 'message-loggedin', {
   
'title': 'Error',
   
'message': format_html(str(form.errors))
 
})

I checked the database and nothing is added to it, form.save() just always returns None. When I try to debug within django/forms/models.py it seems that form.has_changed() always returns False, causing nothing to be saved. However I do not understand why it thinks nothing has been changed. Any help would be greatly appreciated.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Michal Petrucha

unread,
Jul 18, 2018, 5:16:11 AM7/18/18
to Django users
On Tue, Jul 17, 2018 at 12:57:21PM -0700, cbpa...@gmail.com wrote:
> Is there a paid support option to get help? I've been trying to ask for
> help on IRC for a while but all I've gotten is insults.

Reading through the IRC logs, that was a pretty unpleasant exchange,
I'm sorry you were treated like that. As a community, we should do
better.

Anyway, on to your question.

> On Monday, July 16, 2018 at 6:06:00 PM UTC-4, cbpa...@gmail.com wrote:
> >
> > I'm using Django 2.0.3 on Ubuntu 16.04. Any idea what would cause a model
> > formset to return None when calling save()? Here is the relevant code:
> >
> > RepairFormSet = modelformset_factory(Repair, form=RepairForm, exclude=[])
> >
> > form = RepairFormSet(request.POST)
> >
> > if form.is_valid():
> > form.save()
> > return render(request, 'message-loggedin', {
> > 'title': 'Success',
> > 'message': 'A repair request has been made successfully.'
> > })
> > else:
> > return render(request, 'message-loggedin', {
> > 'title': 'Error',
> > 'message': format_html(str(form.errors))
> > })
> >
> >
> > I checked the database and nothing is added to it, form.save()
> > just always returns None. When I try to debug within
> > django/forms/models.py it seems that form.has_changed() always
> > returns False, causing nothing to be saved. However I do not
> > understand why it thinks nothing has been changed. Any help would
> > be greatly appreciated.

Hmm, how do you know that form.save() returns None? Because if the
above is your literal code, then the form.save() call will definitely
return a list – the default implementation of ModelFormSet.save()
always does:
https://github.com/django/django/blob/stable/2.1.x/django/forms/models.py#L657-L669

As for why nothing is created in the database – are you also updating
the management form of the formset on the frontend to indicate that
you have added a new form, and that the formset should process it?

Michal
signature.asc

cbpa...@gmail.com

unread,
Jul 18, 2018, 11:02:22 AM7/18/18
to Django users
You're right. I put a pdb trace in and that function does return an empty list. I previously had a "if not obj" in there, which was being reached, so I assumed that meant it was None, I guess that was just a misunderstanding of the "not" operator on my end.
 
As for why nothing is created in the database – are you also updating
the management form of the formset on the frontend to indicate that
you have added a new form, and that the formset should process it?

 
If by "updating the management form" you're referring to incrementing "form-TOTAL_FORMS", then yes I'm doing that. I've tried leaving it at the default of 1 and submitting only 1 form, and I've tried incrementing it to 2 or 3 and sending more forms along with it... doesn't seem to make a difference. If there's any more information that would be helpful I'd be glad to provide it... I'm not that experienced with formsets and only used them in the admin until now, so it's possible I'm just missing something obvious.

Michal  
Reply all
Reply to author
Forward
0 new messages