A model instances generator for Django
Project description
An easy-to-use implementation of Creation Methods (aka Object Factory) for Django, backed by Faker.
django_fakery will try to guess the field’s value based on the field’s name and type.
Installation
Install with:
$ pip install django-fakery
QuickStart
from django_fakery import factory
factory.make(
'app.Model',
fields={
'field': 'value',
}
)
The value of a field can be any python object, a callable, or a lambda:
from django_fakery import factory
from django.utils import timezone
factory.make(
'app.Model',
fields={
'created': timezone.now
}
)
When using a lambda, it will receive two arguments: n is the iteration number, and f is an instance of faker:
user = factory.make(
'auth.User',
fields={
'username': lambda n, f: 'user_{}'.format(n),
}
)
You can create multiple objects by using the quantity parameter:
from django_fakery import factory
factory.make('app.Model', quantity=4)
For convenience, when the value of a field is a string, it will be interpolated with the iteration number:
user = factory.make(
'auth.User',
fields={
'username': 'user_{}',
},
quantity=4
)
Pre-save and Post-save hooks
You can define functions to be called right before the instance is saved or right after:
from django_fakery import factory
factory.make(
'auth.User',
fields={
'username': 'username',
},
pre_save=[
lambda i: i.set_password('password')
]
)
Since settings a user’s password is such a common case, we special-cased that scenario, so you can just pass it as a field:
from django_fakery import factory
factory.make(
'auth.User',
fields={
'username': 'username',
'password': 'password',
}
)
Lazies
You can refer to the created instance’s own attributes or method 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 import factory, Lazy
factory.make(
'auth.User',
fields={
'username': Lazy('email'),
}
)
If you want to assign a value returned by a method on the instance, you can pass the method’s arguments to the Lazy object:
from django_fakery import factory, Lazy
factory.make(
'myapp.Model',
fields={
'myfield': Lazy('model_method', 'argument', keyword='keyword value'),
}
)
Blueprints
Use a blueprint:
from django_fakery import factory
user = factory.blueprint('auth.User')
user.make(quantity=10)
Blueprints can refer other blueprints:
pizza = factory.blueprint(
'food.Pizza',
fields={
'chef': user,
}
)
Seeding the faker
from django_fakery import factory
factory.make('auth.User', fields={
'username': 'regularuser_{}'
}, seed=1234, quantity=4)
Credits
The API is heavily inspired by model_mommy.
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-1.0.3-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 00ddf774bde7832f7f2bc265aee610120746c655611aaf278cfca8ef62a1ee1e |
|
MD5 | c1760260e013ad54d070503b0c5ab445 |
|
BLAKE2b-256 | 4d5180ce6c171acbf737ce06441bd2523760eda01465742eb0a9d1495ec51bcd |