Skip to content

Conversation

@berinhard
Copy link
Member

Closes #523

This PR introuces the _full_clean flag to every API to create/prepare objects. By default the flag is set to False to not introduce backwards incompatibilities.

@berinhard berinhard requested review from a team and amureki October 31, 2025 16:51
Copy link
Member

@amureki amureki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've got a couple of comments on this PR. And I left a comment on the issue itself, as I am curious more about the context.

Many thanks for the contribution, though!

@berinhard berinhard self-assigned this Nov 6, 2025
Since the bulk create is being done within a transaction block to prevent
objects from being created if invalid error, the number of expected queries
increases by 2 since the transaction runs the statement to register the
rollback checkpoint and the commit one.
@berinhard berinhard requested a review from amureki November 6, 2025 09:25
@berinhard
Copy link
Member Author

Thanks for you review @amureki. I addressed your comments with my new commits 👍

Copy link
Member

@amureki amureki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent fixes, thanks for moving the check and adding the test!
I left some last nitpicky notes, but in general it is good to go!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe, this file should not be commited, as it belongs to a local dev setup.
I think you recently added it to .gitignore, so this is solved on main branch already.

- New flag `_full_clean` to run model validation at created instances (`False` by default)

### Changed
- Bulk creation of models instances is now done within a Django transaction block
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Bulk creation of models instances is now done within a Django transaction block
- Bulk creation now uses transaction blocks to ensure atomic rollback when `_full_clean=True` validation fails

customer = baker.make('shop.Customer', _fill_optional=True)
```

By default, Model Bakery won't run validations on custom fields definitions. To enforce the returned
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for updating the doc!

Shall we also add a small note on atomicity?

When using _full_clean=True with _bulk_create=True, all objects are created within a transaction and will be rolled back if any validation errors occur.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request: Add _full_clean:bool=False to baker.make()

3 participants