diff --git a/CHANGELOG.md b/CHANGELOG.md index b64644b9..7b8f506a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Support for Django 4.0 [PR #236](https://github.com/model-bakers/model_bakery/pull/236) ### Changed +- Explicitly add _fill_optional parameters to baker.make and baker.prepare to aid IDE autocomplete function. [PR #264](https://github.com/model-bakers/model_bakery/pull/264) - Validate `increment_by` parameter of `seq` helper when `value` is an instance of `datetime` [PR #247](https://github.com/model-bakers/model_bakery/pull/247) - Fix a simple typo in `bulk_create` disclaimer in docs [PR #245](https://github.com/model-bakers/model_bakery/pull/245) - Allow relation `_id` fields to use sequences [PR #253](https://github.com/model-bakers/model_bakery/pull/253) diff --git a/model_bakery/baker.py b/model_bakery/baker.py index 6046fa9a..d5b9a5fb 100644 --- a/model_bakery/baker.py +++ b/model_bakery/baker.py @@ -83,6 +83,7 @@ def make( _create_files: bool = False, _using: str = "", _bulk_create: bool = False, + _fill_optional: Union[List[str], bool] = False, **attrs: Any, ) -> List[M]: ... @@ -97,6 +98,7 @@ def make( _create_files: bool = False, _using: str = "", _bulk_create: bool = False, + _fill_optional: Union[List[str], bool] = False, **attrs: Any, ): """Create a persisted instance from a given model its associated models. @@ -105,6 +107,7 @@ def make( fields you want to define its values by yourself. """ _save_kwargs = _save_kwargs or {} + attrs.update({"_fill_optional": _fill_optional}) baker: Baker = Baker.create( _model, make_m2m=make_m2m, create_files=_create_files, _using=_using ) @@ -145,6 +148,7 @@ def prepare( _quantity: int, _save_related: bool = False, _using: str = "", + _fill_optional: Union[List[str], bool] = False, **attrs, ) -> List[M]: ... @@ -155,6 +159,7 @@ def prepare( _quantity: Optional[int] = None, _save_related: bool = False, _using: str = "", + _fill_optional: Union[List[str], bool] = False, **attrs, ): """Create but do not persist an instance from a given model. @@ -162,6 +167,7 @@ def prepare( It fill the fields with random values or you can specify which fields you want to define its values by yourself. """ + attrs.update({"_fill_optional": _fill_optional}) baker = Baker.create(_model, _using=_using) if _valid_quantity(_quantity): raise InvalidQuantityException @@ -363,6 +369,7 @@ def make( _save_kwargs: Optional[Dict[str, Any]] = None, _refresh_after_create: bool = False, _from_manager=None, + _fill_optional: Union[List[str], bool] = False, **attrs: Any, ): """Create and persist an instance of the model associated with Baker instance.""" @@ -372,13 +379,25 @@ def make( "_save_kwargs": _save_kwargs, "_refresh_after_create": _refresh_after_create, "_from_manager": _from_manager, + "_fill_optional": _fill_optional, } params.update(attrs) return self._make(**params) - def prepare(self, _save_related=False, **attrs: Any) -> M: + def prepare( + self, + _save_related=False, + _fill_optional: Union[List[str], bool] = False, + **attrs: Any + ) -> M: """Create, but do not persist, an instance of the associated model.""" - return self._make(commit=False, commit_related=_save_related, **attrs) + params = { + "commit": False, + "commit_related": _save_related, + "_fill_optional": _fill_optional, + } + params.update(attrs) + return self._make(**params) def get_fields(self) -> Any: return set(self.model._meta.get_fields()) - set(self.get_related()) diff --git a/tests/test_baker.py b/tests/test_baker.py index 231dfea8..7a53937b 100644 --- a/tests/test_baker.py +++ b/tests/test_baker.py @@ -635,6 +635,21 @@ def test_skip_blank(self): assert dummy.blank_char_field == "" assert dummy.blank_text_field == "" + def test_skip_blank_with_argument(self): + dummy = baker.make(models.DummyBlankFieldsModel, _fill_optional=False) + assert dummy.blank_char_field == "" + assert dummy.blank_text_field == "" + + def test_skip_blank_when_preparing(self): + dummy = baker.prepare(models.DummyBlankFieldsModel) + assert dummy.blank_char_field == "" + assert dummy.blank_text_field == "" + + def test_skip_blank_when_preparing_with_argument(self): + dummy = baker.prepare(models.DummyBlankFieldsModel, _fill_optional=False) + assert dummy.blank_char_field == "" + assert dummy.blank_text_field == "" + @pytest.mark.django_db class TestFillBlanksTestCase: @@ -644,6 +659,12 @@ def test_fill_field_optional(self): ) assert len(dummy.blank_char_field) == 50 + def test_fill_field_optinal_when_preparing(self): + dummy = baker.prepare( + models.DummyBlankFieldsModel, _fill_optional=["blank_char_field"] + ) + assert len(dummy.blank_char_field) == 50 + def test_fill_wrong_field(self): with pytest.raises(AttributeError) as exc_info: baker.make( @@ -670,6 +691,11 @@ def test_fill_all_optional(self): assert len(dummy.blank_char_field) == 50 assert len(dummy.blank_text_field) == 300 + def test_fill_all_optional_when_preparing(self): + dummy = baker.prepare(models.DummyBlankFieldsModel, _fill_optional=True) + assert len(dummy.blank_char_field) == 50 + assert len(dummy.blank_text_field) == 300 + def test_fill_optional_with_integer(self): with pytest.raises(TypeError): baker.make(models.DummyBlankFieldsModel, _fill_optional=1)