Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #308 -- do not force defaults for make and prepare #330

Merged
merged 4 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## [Unreleased](https://github.com/model-bakers/model_bakery/tree/main)

### Added

### Changed
- Fixed a bug with overwritten `_save_kwargs` and other custom arguments [PR #330](https://github.com/model-bakers/model_bakery/pull/330)

### Removed

## [1.6.0](https://pypi.org/project/model-bakery/1.6.0/)
Expand Down
30 changes: 18 additions & 12 deletions model_bakery/recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,22 +111,28 @@ def make(
def make(
self,
_quantity: Optional[int] = None,
make_m2m: bool = False,
_refresh_after_create: bool = False,
_create_files: bool = False,
make_m2m: Optional[bool] = None,
_refresh_after_create: Optional[bool] = None,
_create_files: Optional[bool] = None,
_using: str = "",
_bulk_create: bool = False,
_bulk_create: Optional[bool] = None,
_save_kwargs: Optional[Dict[str, Any]] = None,
**attrs: Any,
) -> Union[M, List[M]]:
defaults = {
"_quantity": _quantity,
"make_m2m": make_m2m,
"_refresh_after_create": _refresh_after_create,
"_create_files": _create_files,
"_bulk_create": _bulk_create,
"_save_kwargs": _save_kwargs,
}
defaults = {}
if _quantity is not None:
defaults["_quantity"] = _quantity
if make_m2m is not None:
defaults["make_m2m"] = make_m2m
if _refresh_after_create is not None:
defaults["_refresh_after_create"] = _refresh_after_create
if _create_files is not None:
defaults["_create_files"] = _create_files
if _bulk_create is not None:
defaults["_bulk_create"] = _bulk_create
if _save_kwargs is not None:
defaults["_save_kwargs"] = _save_kwargs # type: ignore[assignment]

defaults.update(attrs)
return baker.make(self._model, _using=_using, **self._mapping(_using, defaults))

Expand Down
4 changes: 4 additions & 0 deletions tests/generic/baker_recipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ class SmallDogRecipe(Recipe):

overrided_save = Recipe("generic.ModelWithOverridedSave")

with_save_kwargs = Recipe(
"generic.ModelWithSaveKwargs", _save_kwargs={"breed": "updated_breed"}
)

ip_fields = Recipe(
"generic.DummyGenericIPAddressFieldModel",
ipv4_field=seq("127.0.0.", increment_by=2),
Expand Down
6 changes: 6 additions & 0 deletions tests/generic/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,12 @@ def save(self, *args, **kwargs):
return super(ModelWithOverridedSave, self).save(*args, **kwargs)


class ModelWithSaveKwargs(Dog):
def save(self, *args, **kwargs):
self.breed = kwargs.pop("breed")
return super(ModelWithSaveKwargs, self).save(*args, **kwargs)
amureki marked this conversation as resolved.
Show resolved Hide resolved


class Classroom(models.Model):
students = models.ManyToManyField(Person, null=True)
active = models.BooleanField(null=True)
Expand Down
4 changes: 4 additions & 0 deletions tests/test_recipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,10 @@ def test_pass_save_kwargs(self):
)
assert owner == dog.owner

def test_pass_save_kwargs_in_recipe_definition(self):
dog = baker.make_recipe("tests.generic.with_save_kwargs")
assert dog.breed == "updated_breed"

def test_ip_fields_with_start(self):
first, second = baker.make_recipe("tests.generic.ip_fields", _quantity=2)

Expand Down