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 #513 -- Add missing doc on _refresh_after_create option #514

Merged
merged 1 commit into from
Jan 6, 2025
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 @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## [Unreleased](https://github.com/model-bakers/model_bakery/tree/main)

### Added
- docs: Add missing doc on `_refresh_after_create` option

### Changed
- Fix `Recipe.prepare` without _quantity (on one-to-one relation)
Expand Down
18 changes: 18 additions & 0 deletions docs/basic_usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,24 @@ Model Bakery does not create files for FileField types. If you need to have the

**Important**: the lib does not do any kind of file clean up, so it's up to you to delete the files created by it.

## Refreshing Instances After Creation

By default, Model Bakery does not refresh the instance after it is created and saved.
If you want to refresh the instance after it is created,
you can pass the flag `_refresh_after_create=True` to either `baker.make` or `baker.make_recipe`.
This ensures that any changes made by the database or signal handlers are reflected in the instance.

```python
from model_bakery import baker

# default behavior
customer = baker.make('shop.Customer', birthday='1990-01-01', _refresh_after_create=False)
assert customer.birthday == '1990-01-01'

customer = baker.make('shop.Customer', birthday='1990-01-01', _refresh_after_create=True)
assert customer.birthday == datetime.date(1990, 1, 1)
```

## Non persistent objects

If you don't need a persisted object, Model Bakery can handle this for you as well with the **prepare** method:
Expand Down