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 (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.

QuickStart

from django_fakery.factory 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.factory 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 = Blueprint(
    'auth.User',
    fields={
        'username': lambda n, f: 'user_{}'.format(n),
    }
)

You can create multiple objects by using the quantity parameter: .. code-block:: python

from django_fakery.factory 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 = Blueprint(
    '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.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 arguments 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)

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

  • self-referencing models

Credits

The API is heavily inspired by model_mommy.

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.10.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

django_fakery-0.0.10-py2.py3-none-any.whl (10.0 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

File hashes

Hashes for django-fakery-0.0.10.tar.gz
Algorithm Hash digest
SHA256 79324fb4471eb69eff040f47f1c6c5a3b2b50965261333d3f75ea2f3945214a6
MD5 48427fb9fdecc161ee7987e0cae1d786
BLAKE2b-256 77ad81f4024bfc8e16b7a59e6aa3fd012166facc012e1f2b7589522917bfface

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for django_fakery-0.0.10-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 077c166484bf8e3c30a16c41b397f9e7083ae02ce9283af0a08d3e05ed370186
MD5 684985fd66116b70232aaa6ece0d4190
BLAKE2b-256 50faa91811d7439953aa3a8e220c2e4c2d26a239ee9583fc6486dbfe293a1687

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