Django Model Mommy is a smart object creation facility for Django, providing simple and flexible ways to generate test data for models without having to manually define all fields. It significantly simplifies fixture creation for tests and development. The current version is 2.0.0, with its last release in 2021, indicating it is in maintenance mode rather than active development.
pip install django-model-mommyVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use `mommy.make()` and `mommy.prepare()` to create Django model instances. It includes a minimal, runnable Django setup for demonstration purposes outside of a full project. In a real Django project (e.g., tests), you would typically import your models directly and skip the setup part.
Ensure your project runs on Python 3.x and Django 2.2+ before upgrading `django-model-mommy` to version 2.0.0 or later.
Use `make()` when you need the object persisted and accessible via database queries (e.g., in integration tests). Use `prepare()` when you need an object for validation or to mock database interactions without actually hitting the database.
For unique fields, either provide an explicit unique value (e.g., `mommy.make(MyModel, unique_field='my_specific_value')`) or use a `Recipe` with generators like `mommy.recipe.seq` or custom functions to guarantee uniqueness.
Remove any calls to `mommy.seed()`. For controlling randomness, use Python's standard `random.seed()` if needed for reproducibility.