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.

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
)

Foreign keys

Non-nullable ForeignKey s create related objects automatically.

If you want to explicitly create a related object, you can pass it to the fields like any other value:

pizza = factory.make(
    'food.Pizza',
    fields={
        'chef': factory.make('auth.User', fields={'username': 'Gusteau'}),
    }
)

ManyToManies

Because ManyToManyField``s are implicitly nullable (ie: they're always allowed to have their ``.count() equal to 0), related objects on those fields are not automatically created for you.

If you want to explicitly create a related objects, you can pass a list to the fields like any other value:

pizza = factory.make(
    'food.Pizza',
    fields={
        'toppings': [factory.make('food.Tooping', fields={'name': 'Anchovies'})],
    }
)

You can also pass a factory, to create multiple objects:

pizza = factory.make(
    'food.Pizza',
    fields={
        'toppings': factory.make('food.Tooping', quantity=5),
    }
)

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'),
    }
)

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',
    }
)

Non persistent instances

You can build instances that are not saved to the database by using the .build() method, just like you’d use .make():

from django_fakery import factory

factory.build(
    'app.Model',
    fields={
        'field': 'value',
    }
)

Note that since the instance is not saved to the database, .build() does not support ManyToManies or post-save hooks.

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


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

Uploaded Source

Built Distribution

django_fakery-1.2.8-py2.py3-none-any.whl (15.7 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

File hashes

Hashes for django-fakery-1.2.8.tar.gz
Algorithm Hash digest
SHA256 7c59cc4d7f074e4417855250d6cbd631c2a4d6f7a728d972c5151008dde5dbbf
MD5 0c32853f78dae64e52de3850b821a857
BLAKE2b-256 2ec9799dce1f746faeaccd17986f292ff7cd2cf79c367c62c786aeae2becb2b8

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for django_fakery-1.2.8-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b7e03abffd0bcb260d112c994c23d7bb987b3bf74507032356ce96cbbde2a59f
MD5 5ca4870cfaef653829a510dd92ba3f34
BLAKE2b-256 b7c4660319d60dec5d6f89e7163380fcaa2fc6cc696022653692dafa3cf0ec02

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