Skip to main content

A model instances generator for Django

Project description

https://travis-ci.org/fcurella/django-fakery.svg?branch=master https://coveralls.io/repos/fcurella/django-fakery/badge.svg?branch=master&service=github

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

  • lazy methods

  • post-creation hooks

  • callable shortcuts in bluprints

  • contrib fields

  • localization

  • self-referencing models

License

This software is released under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-fakery-0.0.4.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

django_fakery-0.0.4-py2.py3-none-any.whl (6.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-fakery-0.0.4.tar.gz.

File metadata

File hashes

Hashes for django-fakery-0.0.4.tar.gz
Algorithm Hash digest
SHA256 4f7d065dab3d2bfc889fc17be0d4d2037afc4100198b5fe6614eebe3620ab0a5
MD5 4198a8005f232249520cdfabc83f8b08
BLAKE2b-256 7ea78966f29d04208ae48d172ca823f82efa1f37d751dd4185f655b7f149e0d4

See more details on using hashes here.

Provenance

File details

Details for the file django_fakery-0.0.4-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_fakery-0.0.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 5cb5f09b6140498c8b64d4e239e0407aed57095a25306af102ba60830fcfaf31
MD5 8470e1b372a8505db73e07c3867424ec
BLAKE2b-256 1f2e547ea885d9f9692f154a18936cca2339d8b8cb3c6124d62a24683ff2ad10

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page