Repository: model-bakers/model_bakery

Issue:
`make()` with `_bulk_create=True` does not create M2M-entries that are specified using the reverse/related name.
When using `baker.make` with `_bulk_create=True`, it will silently ignore arguments that should be used to fill many-to-many fields if the arguments use the reverve/related name.

Follow up to #298, where the same issue was fixed when using the forward name of a m2m relationship.

**To Reproduce**
with
```python3
class House(models.Model):
     pass

class HouseDetail(models.Model):
    houses = models.ManyToManyField(House, blank=True)
```

this snippet fails at the assertion:
```python3
detail = baker.make(HouseDetail)
houses = baker.make(House, housedetail_set=[detail], _quantity=20, _bulk_create=True)
assert houses[0].housedetail_set.count() == 1
```

**Expected behavior**
For each house instance in `houses`, the `housedetail_set` should contain the `detail`, and thus have `count() == 1`. This also happens when `_bulk_create=False` is used.

**Versions**
 - Python: [3.10.6]
 - Django [4.1.6]
 - Model Bakery [1.9.0]


Discussion:

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c9862b1..b4d09e0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
 ### Added
 
 ### Changed
+- Handle bulk creation when using reverse related name
 
 ### Removed
 
diff --git a/model_bakery/baker.py b/model_bakery/baker.py
index 2723ece..7dd24da 100644
--- a/model_bakery/baker.py
+++ b/model_bakery/baker.py
@@ -864,4 +864,18 @@ def bulk_create(baker: Baker[M], quantity: int, **kwargs) -> List[M]:
                         for obj in kwargs[field.name]
                     ]
                 )
+
+        # set many-to-many relations that are specified using related name from kwargs
+        for field in baker.model._meta.get_fields():
+            if field.many_to_many and hasattr(field, "related_model"):
+                reverse_relation_name = (
+                    field.related_query_name
+                    or field.related_name
+                    or f"{field.related_model._meta.model_name}_set"
+                )
+                if reverse_relation_name in kwargs:
+                    getattr(entry, reverse_relation_name).set(
+                        kwargs[reverse_relation_name]
+                    )
+
     return created_entries


Test patch:
diff --git a/tests/test_baker.py b/tests/test_baker.py
index 6532343..e0c4874 100644
--- a/tests/test_baker.py
+++ b/tests/test_baker.py
@@ -1056,8 +1056,8 @@ class TestBakerMakeCanFetchInstanceFromDefaultManager:
         assert movie.title == movie.name
 
 
+@pytest.mark.django_db
 class TestCreateM2MWhenBulkCreate(TestCase):
-    @pytest.mark.django_db
     def test_create(self):
         query_count = 12
         with self.assertNumQueries(query_count):
@@ -1068,6 +1068,19 @@ class TestCreateM2MWhenBulkCreate(TestCase):
         c1, c2 = models.Classroom.objects.all()[:2]
         assert list(c1.students.all()) == list(c2.students.all()) == [person]
 
+    def test_make_should_create_objects_using_reverse_name(self):
+        classroom = baker.make(models.Classroom)
+
+        with self.assertNumQueries(21):
+            students = baker.make(
+                models.Person,
+                classroom_set=[classroom],
+                _quantity=10,
+                _bulk_create=True,
+            )
+
+        assert students[0].classroom_set.count() == 1
+
 
 class TestBakerSeeded:
     @pytest.fixture()