Model Bakery
Django model test data generator — creates Django model instances with automatically populated fields for testing. Model Bakery (successor to Model Mommy) features: baker.make('myapp.Agent') creates Agent instance with random valid data for all fields, baker.make('Agent', name='My Agent') overrides specific fields, baker.prepare() creates unsaved instances, baker.make_recipe() for named recipes in baker_recipes.py, M2M and FK relationships auto-created, custom value generators, and bulk creation via _quantity parameter. Eliminates boilerplate Agent.objects.create(name='test', status='active', ...) in Django agent tests by auto-filling required fields.
Score Breakdown
⚙ Agent Friendliness
🔒 Security
Testing library — no production security concerns. Generated agent test data uses random values; ensure tests don't accidentally use predictable test data for security testing. Never use baker.make() in production management commands — test data generation library only.
⚡ Reliability
Best When
You're writing Django agent tests and don't want to manually specify all required model fields in every test — Model Bakery auto-fills fields so you only specify what matters for each agent test case.
Avoid When
You need complex factory hierarchies with dependent computed values, non-Django ORM, or production data seeding.
Use Cases
- • Agent model test data — baker.make('agents.Agent', name='Test Agent') creates agent with auto-populated required fields (id, created_at, status, etc.); no need to specify all required fields in every agent test
- • Related agent objects — baker.make('agents.AgentTask', agent=baker.make('agents.Agent')) auto-creates related agent when not specified; baker.make('AgentTask', _quantity=5) creates 5 related agent tasks
- • Agent test recipes — baker_recipes.py with Recipe('agent', status='active', model='gpt-4') defines named agent fixtures; baker.make_recipe('agents.active_agent') in tests creates pre-configured agent; shareable test data patterns across agent test suite
- • Unsaved agent preparation — baker.prepare('Agent', name='Draft') creates Agent instance without DB write; useful for testing agent serializers, validators, and form handling without touching database
- • Bulk agent test data — baker.make('Agent', _quantity=100, status=cycle(['active', 'pending', 'inactive'])) creates 100 agents cycling through statuses; agent list view and pagination tests with realistic mixed-status datasets
Not For
- • Non-Django ORMs — Model Bakery is Django ORM-specific; for SQLAlchemy agent test data use factory_boy with SQLAlchemy support
- • Complex dependent data scenarios — for agent test data with complex business rules and dependent states, factory_boy's SubFactory and LazyAttribute provide more control
- • Production data seeding — Model Bakery generates random test data; for production seed data with specific values use Django fixtures or custom management commands
Interface
Authentication
No auth — Django test data library.
Pricing
Model Bakery is Apache 2.0 licensed. Free for all use.
Agent Metadata
Known Gotchas
- ⚠ baker vs baker.make import confusion — from model_bakery import baker then baker.make() is correct; some tutorials show from model_bakery.baker import make then make() directly; both work but mixing styles in agent test files causes NameError; standardize import pattern across agent test suite
- ⚠ Auto-generated values may violate unique constraints — baker.make('Agent', _quantity=5) with unique=True field generates 5 random values; occasionally generates duplicate causing IntegrityError; use baker.make('Agent', name=seq('Agent-')) or explicit unique values for agent fields with unique constraint
- ⚠ M2M fields require post-create assignment — baker.make('AgentTag', agents=[agent1, agent2]) won't work for M2M directly on make(); use baker.make('AgentTag') then result.agents.set([agent1, agent2]) for agent M2M relationships; Model Bakery auto-creates M2M objects but doesn't add them to existing instances without explicit set()
- ⚠ baker.prepare() doesn't save FK objects — baker.prepare('AgentTask') creates unsaved AgentTask with unsaved FK Agent; saving AgentTask.save() fails because Agent has no pk; use baker.make() for FK dependencies even when baker.prepare() for the target model; or pass explicit saved FK instances to baker.prepare()
- ⚠ Recipe _create_many field requires list not queryset — baker.make_recipe('agents.task_recipe', agents=Agent.objects.filter(active=True)) fails; convert to list: agents=list(Agent.objects.filter(active=True)); agent test recipes using queryset for M2M relations cause unexpected TypeError
- ⚠ Custom field generators override all instances of type — baker.generators.add(CustomField, lambda: 'default') applies to ALL CustomField instances in test session; registering generator in setUp() without cleanup affects other agent tests in same session; register generators in conftest.py or use autouse fixture with teardown
Alternatives
Full Evaluation Report
Detailed scoring breakdown, competitive positioning, security analysis, and improvement recommendations for Model Bakery.
Scores are editorial opinions as of 2026-03-06.