A model instances generator for Django
Project description
An easy-to-use implementation of Creation Methods for Django, backed by Faker.
django_fakery will try to guess the field’s value based on the field’s name and type.
QuickStart
from django_fakery.factory import factory
factory.make('auth.User', quantity=4)
Lazies
You can refer to the created instance’s own field by using Lazy objects.
For example, if you’d like to create user with email as username, and have them always match, you could do:
from django_fakery.lazy import Lazy
factory.make(
'auth.User',
username=Lazy('email')
)
Blueprints
from django_fakery.blueprint import Blueprint
user = Blueprint('auth.User')
user.make(quantity=10)
If you want to ensure uniqueness when generating multiple objects, you can use a lambda function.
In this example, n is the iteration number, and f is an instance of faker:
user = Blueprint(
'auth.User',
fields={
'username': lambda n, f: 'user_%s' % n,
}
)
For convenience, when the value is a string, you can simply pass a formatting string:
user = Blueprint(
'auth.User',
fields={
'username': 'user_%(n)s',
}
)
Blueprints can refer other blueprints:
pizza = Blueprint(
'food.Pizza',
fields={
'chef': user,
}
)
Seeding the faker
from django_fakery.factory import Factory
factory = Factory()
factory.seed(1234)
factory.make('auth.User', fields={
'username': 'regularuser'
}, seed=1234, quantity=4)
from django_fakery.factory import factory
factory.make('auth.User', fields={
'username': 'regularuser'
}, seed=1234, quantity=4)
TODO
whitelist field name
lazy methods
post-creation hooks
callable shortcuts in bluprints
License
This software is released under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django_fakery-0.0.3-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a51f49673ba9ecc66b395c5b913eb1e69c1c3730edad0f324a695d0da528bb29 |
|
MD5 | 34a662572a3165ece968406f9d6e1ba6 |
|
BLAKE2b-256 | b33a099dcda92dcdf29a190fac8440c9b861b06838c9f4ad355363d7bce882c7 |