Registry / testing / django-model-mommy

django-model-mommy

JSON →
library2.0.0pypypiunverified

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-mommy
INSTALL
IMPORT
SIG · DJANGO-MODEL-MOMMY
D
django-model-mommy
testingpythonv2.0.0
Install
Import
Disk
Pass rate
0/ 10
Env Coverage0 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v? · pip install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
build_error
glibc
py 3.103.920 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

mommy
from model_mommy import mommy
from mommy import mommy
The top-level module is `model_mommy`, not just `mommy`.
Recipe
from model_mommy.recipe import Recipe

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.

import os import django from django.conf import settings from django.db import models # Minimal Django setup for standalone execution if not settings.configured: os.environ.setdefault('DJANGO_SETTINGS_MODULE', __name__) settings.configure( INSTALLED_APPS=[ 'django.contrib.auth', 'django.contrib.contenttypes', 'myapp' # assuming 'myapp' will be created ], DATABASES={'default': {'ENGINE': 'django.db.backends.sqlite3', 'NAME': ':memory:'}}, DEBUG=True ) django.setup() # Define a simple Django model for demonstration class Author(models.Model): name = models.CharField(max_length=100, unique=True) email = models.EmailField() bio = models.TextField(blank=True, null=True) class Meta: app_label = 'myapp' def __str__(self): return self.name # To ensure the model is 'migrated' in memory for mommy from django.core.management import call_command call_command('makemigrations', 'myapp', interactive=False, verbosity=0) call_command('migrate', 'myapp', interactive=False, verbosity=0) from model_mommy import mommy # 1. Create a single Author instance with random data author_1 = mommy.make(Author) print(f"1. Created author (random): {author_1.name} <{author_1.email}>") # 2. Create multiple Author instances (e.g., 3) authors = mommy.make(Author, _quantity=3) print(f"2. Created {len(authors)} authors (random). Example: {authors[0].name}") # 3. Create an Author instance with specific attributes specific_author = mommy.make(Author, name='Jane Doe', email='jane.doe@example.com') print(f"3. Created author (specific): {specific_author.name} <{specific_author.email}>") # 4. Use mommy.prepare() to create an unsaved instance unsaved_author = mommy.prepare(Author, name='Unsaved Sam') print(f"4. Prepared unsaved author: {unsaved_author.name} (not in DB yet)") # You can then save it manually if needed unsaved_author.save() print(f" Saved unsaved author to DB.")
Debug
Known issues
breakingPython 2 and Older Django Versions No Longer Supported. Version 2.0.0 and above dropped support for Python 2.x and Django versions prior to 2.2.
fix
Ensure your project runs on Python 3.x and Django 2.2+ before upgrading `django-model-mommy` to version 2.0.0 or later.
affects: >=2.0.0
gotcha`mommy.make()` vs. `mommy.prepare()`: `make()` saves the created object(s) to the database, including related objects, while `prepare()` creates an unsaved model instance, useful for validation or manual saving.
fix
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.
affects: All
gotchaUnique Field Collisions: `mommy` generates random data for fields, which can lead to `IntegrityError` if a generated value for a `unique=True` field already exists in the database. This is more common with simple data types or small ranges.
fix
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.
affects: All
deprecatedThe `mommy.seed()` function for seeding the random number generator was removed in version 2.0.0.
fix
Remove any calls to `mommy.seed()`. For controlling randomness, use Python's standard `random.seed()` if needed for reproducibility.
affects: <2.0.0
Upgrade
Version history
2.0.0latest on PyPI
Audit
Dependencies
DjangorequiredCore functionality is built on Django's ORM and model system.
Agent activity
4 hits · last 30 days
node
4
Resources
django-model-mommy — pip install django-model-mommy · libregistry