Object factory for Django
Model Bakery: Smart fixtures for better tests
Model Bakery offers you a smart way to create fixtures for testing in Django. With a simple and powerful API, you can create many objects with a single line of code.
Installation
pip install model-bakery
Supported Versions
Model Bakery follows the Python and Django release and support cycles. We drop support for Python and Django versions when they reach end-of-life.
Basic usage
# models.py
from django.db import models
class Customer(models.Model): name = models.CharField(max_length=30) email = models.EmailField() age = models.IntegerField() isjardsmacale_fan = models.BooleanField() bio = models.TextField() birthday = models.DateField() last_shopping = models.DateTimeField()
test_models.py
from django.test import TestCase
from model_bakery import baker
class TestCustomerModel(TestCase): def setUp(self): self.customer = baker.make('shop.Customer') print(self.customer.dict)
""" {'_state': <django.db.models.base.ModelState object at 0x1129a3240>, 'age': 3841, 'bio': 'vUFzMUMyKzlnTyiCxfgODIhrnkjzgQwHtzIbtnVDKflqevczfnaOACkDNqvCHwvtWdLwoiKrCqfppAlogSLECtMmfleeveyqefkGyTGnpbkVQTtviQVDESpXascHAluGHYEotSypSiHvHzFteKIcUebrzUVigiOacfnGdvijEPrZdSCIIBjuXZMaWLrMXyrsUCdKPLRBRYklRdtZhgtxuASXdhNGhDsrnPHrYRClhrSJSVFojMkUHBvSZhoXoCrTfHsAjenCEHvcLeCecsXwXgWJcnJPSFdOmOpiHRnhSgRF', 'birthday': datetime.date(2019, 12, 3), 'email': 'rpNATNsxoj@example.com', 'isjardsmacale_fan': True, 'id': 1, 'last_shopping': datetime.datetime(2019, 12, 3, 21, 42, 34, 77019), 'name': 'qiayYnESvqcYLLBzxpFOcGBIfnQEPx'} """
Documentation
For more detailed information, check out the full documentation.
Contributing
As an open-source project, Model Bakery welcomes contributions of many forms:
- Code patches
- Documentation improvements
- Bug reports
Maintainers
Creator
Sponsors
License
Model Bakery is licensed under Apache License 2.0. See the LICENSE file for more information.