{"id":9117,"library":"mixer","title":"Mixer","description":"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.","status":"active","version":"7.2.2","language":"en","source_language":"en","source_url":"https://github.com/klen/mixer","tags":["fixtures","testing","orm","mocking","test-data","faker","django","sqlalchemy","mongoengine"],"install":[{"cmd":"pip install mixer","lang":"bash","label":"Install core library"},{"cmd":"pip install mixer[django]","lang":"bash","label":"Install with Django support"},{"cmd":"pip install mixer[sqlalchemy]","lang":"bash","label":"Install with SQLAlchemy support"},{"cmd":"pip install mixer[mongoengine]","lang":"bash","label":"Install with MongoEngine support"}],"dependencies":[{"reason":"Required for Django ORM integration","package":"Django","optional":true},{"reason":"Required for SQLAlchemy ORM integration","package":"SQLAlchemy","optional":true},{"reason":"Required for MongoEngine ODM integration","package":"MongoEngine","optional":true}],"imports":[{"note":"For creating plain Python objects or using custom backends. The top-level 'mixer' module does not expose a callable Mixer directly.","wrong":"import mixer","symbol":"Mixer","correct":"from mixer.mix import Mixer"},{"note":"The 'mixer.main' module was removed in v7. Use specific backend imports.","wrong":"from mixer.main import mixer","symbol":"mixer (Django backend)","correct":"from mixer.backend.django import mixer"},{"note":"Direct import from 'mixer' is ambiguous; use specific backend imports for ORM integration.","wrong":"from mixer import mixer","symbol":"mixer (SQLAlchemy backend)","correct":"from mixer.backend.sqlalchemy import mixer"}],"quickstart":{"code":"from mixer.mix import Mixer\n\nclass User:\n    def __init__(self, name: str, email: str, age: int):\n        self.name = name\n        self.email = email\n        self.age = age\n\n    def __repr__(self):\n        return f\"User(name='{self.name}', email='{self.email}', age={self.age})\"\n\nmixer = Mixer() # Initialize a mixer instance for plain Python objects\n\n# Create a single user with random data\nuser_instance = mixer.blend(User)\nprint(f\"Random User: {user_instance}\")\n\n# Create a user with specific fields\nspecific_user = mixer.blend(User, name='Alice', age=30)\nprint(f\"Specific User: {specific_user}\")\n\n# Create multiple unique users\nusers_list = mixer.cycle(3, User)\nprint(\"\\nMultiple Users:\")\nfor user in users_list:\n    print(user)\n","lang":"python","description":"This quickstart demonstrates how to initialize the `Mixer` for plain Python objects and use `mixer.blend` to create single instances, and `mixer.cycle` to create multiple unique instances, with or without overriding specific fields."},"warnings":[{"fix":"Migrate your imports to use backend-specific instances (e.g., `from mixer.backend.django import mixer`) or explicitly instantiate `Mixer` for plain objects (`from mixer.mix import Mixer; mixer_instance = Mixer()`).","message":"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.","severity":"breaking","affected_versions":"7.0.0 and later"},{"fix":"Always import the correct mixer backend for your ORM: `from mixer.backend.django import mixer` for Django, `from mixer.backend.sqlalchemy import mixer` for SQLAlchemy, etc.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Instead of `mixer.blend(User, email=lorem.email())`, use `mixer.blend(User, email=lorem.email)`. If you need to pass arguments to the faker, use a lambda: `email=lambda: lorem.email(domain='example.com')`.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For non-nullable foreign keys, either ensure the related model can be created by Mixer successfully, or explicitly pass an existing instance of the related object: `mixer.blend(MyModel, foreign_key_field=existing_related_instance)`.","message":"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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"For plain Python objects: `from mixer.mix import Mixer; my_mixer = Mixer(); my_mixer.blend(...)`. For ORMs: `from mixer.backend.django import mixer; mixer.blend(...)`.","cause":"You likely tried `import mixer` and then `mixer.blend(...)` without initializing a `Mixer` instance or using a specific backend.","error":"AttributeError: module 'mixer' has no attribute 'blend'"},{"fix":"Update your import statement to use the specific backend, e.g., `from mixer.backend.django import mixer` or `from mixer.backend.sqlalchemy import mixer`.","cause":"You are using an old import path from Mixer v6 or earlier that is no longer valid in v7+.","error":"ImportError: cannot import name 'mixer' from 'mixer.main' (D:\\...\\mixer\\main.py)"},{"fix":"Remove the parentheses when passing faker functions: `mixer.blend(User, email=lorem.email)`.","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`.","error":"TypeError: 'str' object is not callable (or similar for other types like 'int' object is not callable)"},{"fix":"Ensure 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)`.","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.","error":"django.db.utils.IntegrityError: NOT NULL constraint failed: myapp_mymodel.related_id"}]}