Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Added

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

### Removed

Expand Down
5 changes: 3 additions & 2 deletions model_bakery/baker.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,9 @@ def make(
if _valid_quantity(_quantity):
raise InvalidQuantityException

if _quantity and _bulk_create:
return bulk_create(baker, _quantity, _save_kwargs=_save_kwargs, **attrs)
if _bulk_create:
result = bulk_create(baker, _quantity or 1, _save_kwargs=_save_kwargs, **attrs)
return result if _quantity else result[0]
elif _quantity:
return [
baker.make(
Expand Down
13 changes: 13 additions & 0 deletions tests/test_baker.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,19 @@ def test_make_quantity_respecting_bulk_create_parameter(self):
)
assert models.NonStandardManager.manager.count() == 3

@pytest.mark.django_db
def test_make_bulk_create_without_quantity(self):
"""Issue #462: _bulk_create=True without _quantity should use bulk_create."""
with patch.object(
models.Person._base_manager,
"bulk_create",
wraps=models.Person._base_manager.bulk_create,
) as mock_bulk:
person = baker.make(models.Person, _bulk_create=True)
mock_bulk.assert_called_once()
assert isinstance(person, models.Person)
assert person.pk is not None

@pytest.mark.django_db
def test_make_raises_correct_exception_if_invalid_quantity(self):
with pytest.raises(InvalidQuantityException):
Expand Down