﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
31539	Add support for bulk operations on reverse many-to-one manager	Baptiste Mispelon	Rahul Biswas	"Using the `Book` and `Author` models from the docs:
{{{#!python
class Author(models.Model):
    name = models.CharField(max_length=100)

class Book(models.Model):
    author = models.ForeignKey(Author, on_delete=models.CASCADE, related_name='books')
    title = models.CharField(max_length=200)
}}}

This works and creates two books linked to their author correctly:
{{{#!python
toni = Author.objects.create(name=""Toni Morrison"")

toni.books.create(title=""The Bluest Eye"")
toni.books.create(title=""Beloved"")
}}}

But if you try to use `bulk_create` to achieve the same, you get an error:
{{{#!python
toni = Author.objects.create(name=""Toni Morrison"")

toni.books.bulk_create([
    Book(title=""The Bluest Eye""),
    Book(title=""Beloved""),
])
}}}

This fails with an `IntegrityError` because `bulk_create` doesn't automatically fill in the value of `Book.author` (the way it does it for `create()`).


The documentation for `RelatedManager` [1] doesn't mention `bulk_create()` but it's not clear to me if that means the operation is unsupported or not (other methods like `count()` or `filter()` are not listed either but are clearly supported). Because of this I wasn't sure whether to mark this ticket as a bug or as a new feature.

Looking at the code [2], I can't find an explanation of why `bulk_create` or `bulk_update` are not implemented either.
I've come up with the following implementation which seems to work (I haven't tested it extensively):

{{{#!python
def bulk_create(self, objs, **kwargs):
    def set_field_to_instance(instance):
        setattr(instance, self.field.name, self.instance)
        return instance

    objs = map(set_field_to_instance, objs)
    db = router.db_for_write(self.model, instance=self.instance)
    return super(RelatedManager, self.db_manager(db)).bulk_create(objs, **kwargs)
}}}

[1] https://docs.djangoproject.com/en/dev/ref/models/relations/#django.db.models.fields.related.RelatedManager
[2] https://github.com/django/django/blob/aff7a58aef0264e5b2740e5df07894ecc0d7a580/django/db/models/fields/related_descriptors.py#L559"	New feature	assigned	Database layer (models, ORM)	dev	Normal				Accepted	0	0	0	0	0	0
