[Django] #29256: Needs two save() calls

7 views
Skip to first unread message

Django

unread,
Mar 24, 2018, 5:51:25 PM3/24/18
to django-...@googlegroups.com
#29256: Needs two save() calls
-------------------------------------+-------------------------------------
Reporter: Victor | Owner: nobody
Porton |
Type: Bug | Status: new
Component: Database | Version: 1.11
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
As http://guido.vonrudorff.de/django-manytomany-relationship-and-unsaved-
objects/ teaches (and our tests show too), `save()` should be called twice
when using m2m fields:

{{{
parts = BarClass.objects.filter(label__in=['eggs', 'bacon', 'spam'])
foo = FooClass(label='lunch')
foo.save() # do not forget this line
foo.child = parts
foo.save()
}}}

It looks silly and counter-intuitive to call `save()` twice. Is this a
bug?

At least the docs should say this vividly. So at very least we have a bug
in documentation for not addressing this issue.

The model code used:

{{{
class BarClass(models.Model):
label = models.CharField()

class FooClass(models.Model):
label = models.CharField()
child = models.ManyToManyField(BarClass)
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/29256>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 24, 2018, 5:53:54 PM3/24/18
to django-...@googlegroups.com
#29256: Needs two save() calls
-------------------------------------+-------------------------------------
Reporter: Victor Porton | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Victor Porton):

* cc: Victor Porton (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/29256#comment:1>

Django

unread,
Mar 24, 2018, 7:22:57 PM3/24/18
to django-...@googlegroups.com
#29256: Need a save() call after assigning a many to many relation

-------------------------------------+-------------------------------------
Reporter: Victor Porton | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* status: new => closed
* resolution: => invalid


Comment:

The blog post is incorrect. A second save isn't needed. Also, that code
won't work in Django 2.0 and later which removed support for
[https://docs.djangoproject.com/en/dev/releases/1.10/#direct-assignment-
to-a-reverse-foreign-key-or-many-to-many-relation direct assignment to
many to many relations].

The blog post should use `foo.child.set(parts)` as described
[https://docs.djangoproject.com/en/stable/topics/db/examples/many_to_many/
in the documentation].

In the future, please use [wiki:TicketClosingReasons/UseSupportChannels
our support channels] to ask "is it a bug?" questions.

--
Ticket URL: <https://code.djangoproject.com/ticket/29256#comment:2>

Reply all
Reply to author
Forward
0 new messages