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)

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.lazy import Lazy

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.lazy import Lazy

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.lazy import 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 argument to the Lazy object:

from django_fakery.lazy import Lazy

factory.make(
    'myapp.Model',
    fields={
        'myfield': Lazy('model_method', 'argument', keyword='keyword value'),
    }
)

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

  • callable shortcuts

  • 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.5.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

django_fakery-0.0.5-py2.py3-none-any.whl (6.9 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

File hashes

Hashes for django-fakery-0.0.5.tar.gz
Algorithm Hash digest
SHA256 f8ffde42cdc6deb0d4bed58319475315e04027f54552a3f35e0257099159340e
MD5 0f10aceccac0f161cdc15ac84cfc5981
BLAKE2b-256 f8a56524cb37171174adae864c192f1cec209444fdbf9a124a30048070940e5a

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for django_fakery-0.0.5-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 60563456f09a33ce3c0a039edfe7df0a0f154b41bdb9a60f2860b21cbce7143e
MD5 a8e1a40f93933ea154d701ed9460fb7b
BLAKE2b-256 e89d2f18d9e8a7865c602e81f63df249d9a97efe89e0db12db805cb4848a3361

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