Skip to content

Commit

Permalink
Fix #513 -- Add missing doc on _refresh_after_create option (#514)
Browse files Browse the repository at this point in the history
  • Loading branch information
amureki authored Jan 6, 2025
1 parent aa2ab44 commit 0e49b20
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
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

0 comments on commit 0e49b20

Please sign in to comment.