Installation

Install with:

$ pip install django-fakery

QuickStart

from django_fakery import factory
from myapp.models import MyModel

factory.m('app.Model')(field='value')

If you use pytest, you can use the fakery fixture (requires pytest and pytest-django):

import pytest
from myapp.models import MyModel

@pytest.mark.django_db
def test_mymodel(fakery):
    fakery.m(MyModel)(field='value')

If you’d rather, you can use a more wordy API:

from django_fakery import factory
from myapp.models import MyModel

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

We will use the short API throught the documentation.

The value of a field can be any python object, a callable, or a lambda:

from django.utils import timezone
from django_fakery import factory
from myapp.models import MyModel

factory.m(MyModel)(created=timezone.now)

When using a lambda, it will receive two arguments: n is the iteration number, and f is an instance of faker:

from django.contrib.auth.models import User

user = factory.m(User)(
    username=lambda n, f: 'user_{}'.format(n),
)

django-fakery includes some pre-built lambdas for common needs. See Shortcuts for more info.

You can create multiple objects by using the quantity parameter:

from django_fakery import factory
from myapp.models import MyModel

factory.m(MyModel, quantity=4)

For convenience, when the value of a field is a string, it will be interpolated with the iteration number:

from django.contrib.auth.models import User

user = factory.m(User, quantity=4)(
    username='user_{}',
)