Mixer
Mixer is a Python library designed as a fixtures replacement, simplifying the creation of test data. It supports various ORMs like Django ORM, SQLAlchemy ORM, MongoEngine ODM, and plain Python objects. The current version is 7.2.2. It has a moderate release cadence, with updates and bug fixes released periodically.
Common errors
-
AttributeError: module 'mixer' has no attribute 'blend'
cause You likely tried `import mixer` and then `mixer.blend(...)` without initializing a `Mixer` instance or using a specific backend.fixFor plain Python objects: `from mixer.mix import Mixer; my_mixer = Mixer(); my_mixer.blend(...)`. For ORMs: `from mixer.backend.django import mixer; mixer.blend(...)`. -
ImportError: cannot import name 'mixer' from 'mixer.main' (D:\...\mixer\main.py)
cause You are using an old import path from Mixer v6 or earlier that is no longer valid in v7+.fixUpdate your import statement to use the specific backend, e.g., `from mixer.backend.django import mixer` or `from mixer.backend.sqlalchemy import mixer`. -
TypeError: 'str' object is not callable (or similar for other types like 'int' object is not callable)
cause You passed the *result* of a faker function (e.g., `lorem.email()`) instead of the faker callable itself (`lorem.email`) to `mixer.blend` or `mixer.cycle`.fixRemove the parentheses when passing faker functions: `mixer.blend(User, email=lorem.email)`. -
django.db.utils.IntegrityError: NOT NULL constraint failed: myapp_mymodel.related_id
cause Mixer attempted to create a related object for a non-nullable ForeignKey, but either the creation failed, or no valid related object could be associated.fixEnsure the related model is configured such that Mixer can create valid instances, or explicitly provide an existing related object: `mixer.blend(MyModel, related_field=existing_related_instance)`.
Warnings
- breaking The `mixer.main.mixer` module and `mixer.mix.Mixer` (as a top-level callable) were removed or significantly changed. Direct imports like `from mixer.main import mixer` will fail.
- gotcha Using the wrong `mixer` instance for your ORM backend (e.g., using `mixer.mix.Mixer` instance for a Django model) will not correctly integrate with the ORM's save methods or relationships, potentially leading to validation errors or incorrect data.
- gotcha When providing callables (like `mixer.fakers` functions) to `mixer.blend` or `mixer.cycle`, pass the callable itself, not the result of calling it. Mixer will invoke the callable for each new object.
- gotcha Mixer attempts to create related objects for foreign keys automatically. If a related object is not explicitly provided, and the foreign key is non-nullable, this can lead to `IntegrityError` if the automatically created related object is invalid or fails to save.
Install
-
pip install mixer -
pip install mixer[django] -
pip install mixer[sqlalchemy] -
pip install mixer[mongoengine]
Imports
- Mixer
import mixer
from mixer.mix import Mixer
- mixer (Django backend)
from mixer.main import mixer
from mixer.backend.django import mixer
- mixer (SQLAlchemy backend)
from mixer import mixer
from mixer.backend.sqlalchemy import mixer
Quickstart
from mixer.mix import Mixer
class User:
def __init__(self, name: str, email: str, age: int):
self.name = name
self.email = email
self.age = age
def __repr__(self):
return f"User(name='{self.name}', email='{self.email}', age={self.age})"
mixer = Mixer() # Initialize a mixer instance for plain Python objects
# Create a single user with random data
user_instance = mixer.blend(User)
print(f"Random User: {user_instance}")
# Create a user with specific fields
specific_user = mixer.blend(User, name='Alice', age=30)
print(f"Specific User: {specific_user}")
# Create multiple unique users
users_list = mixer.cycle(3, User)
print("\nMultiple Users:")
for user in users_list:
print(user)