{{{
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.
* cc: Victor Porton (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/29256#comment:1>
* 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>