Skip to content

Commit 8f6c9b7

Browse files
authored
Fix #462 -- Allow _bulk_create=True without _quantity (#571)
1 parent f47bbfb commit 8f6c9b7

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1010
### Added
1111

1212
### Changed
13+
- Allow `_bulk_create=True` without `_quantity` ([#462](https://github.com/model-bakers/model_bakery/issues/462))
1314

1415
### Removed
1516

model_bakery/baker.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,9 @@ def make(
135135
if _valid_quantity(_quantity):
136136
raise InvalidQuantityException
137137

138-
if _quantity and _bulk_create:
139-
return bulk_create(baker, _quantity, _save_kwargs=_save_kwargs, **attrs)
138+
if _bulk_create:
139+
result = bulk_create(baker, _quantity or 1, _save_kwargs=_save_kwargs, **attrs)
140+
return result if _quantity else result[0]
140141
elif _quantity:
141142
return [
142143
baker.make(

tests/test_baker.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,19 @@ def test_make_quantity_respecting_bulk_create_parameter(self):
182182
)
183183
assert models.NonStandardManager.manager.count() == 3
184184

185+
@pytest.mark.django_db
186+
def test_make_bulk_create_without_quantity(self):
187+
"""Issue #462: _bulk_create=True without _quantity should use bulk_create."""
188+
with patch.object(
189+
models.Person._base_manager,
190+
"bulk_create",
191+
wraps=models.Person._base_manager.bulk_create,
192+
) as mock_bulk:
193+
person = baker.make(models.Person, _bulk_create=True)
194+
mock_bulk.assert_called_once()
195+
assert isinstance(person, models.Person)
196+
assert person.pk is not None
197+
185198
@pytest.mark.django_db
186199
def test_make_raises_correct_exception_if_invalid_quantity(self):
187200
with pytest.raises(InvalidQuantityException):

0 commit comments

Comments
 (0)