Skip to main content

App to generate data for django apps using factory-boy factories.

Table of contents

Installation

pip install django-factory-boy-generator

Add factory_generator to your INSTALLED_APPS setting.

INSTALLED_APPS = [
    ...
    'factory_generator',
]

Requirements

  • Python 3.6 or newer
  • Django >= 1.11 or newer
  • factory-boy >= 3.2 or newer

Usage

  1. Create in your django app(s) module factories.py.
  2. Define in this module factories according to factory-boy docs. All factories for a Django Model should use the DjangoModelFactory base class.

Example:

# sample_app/models.py
from django.db import models

class Person(models.Model):
    name = models.CharField(max_length=64)
    phone = models.CharField(max_length=12)
    email = models.CharField(max_length=64)


# sample_app/factories.py

from sample_app.models import Person
import factory

class PersonFactory(factory.django.DjangoModelFactory):
    name = factory.Faker('first_name')
    phone = factory.Sequence(lambda n: '123-555-%04d' % n)
    email = factory.LazyAttribute(lambda obj: '%s@example.com' % obj.name)

Use django-admin commands:

  • Generate records in database:

python manage.py generate_to_db [app_label[.FactoryName] [app_label[.FactorylName] ...]]

  • Generate data to json:

python manage.py generate_to_json [app_label[.FactoryName] [app_label[.FactorylName] ...]]

Options:

--exclude EXCLUDE, -e EXCLUDE

Prevents specific applications or factories (specified in the form of app_label.FactoryName) from being generated. If you specify a factory name, the result will be restricted to that factory, rather than the entire application. You can also mix application names and factory names.

If you want to exclude multiple applications, pass --exclude more than once:

--quantity QUANTITY, -q QUANTITY

Quantity of objects every factory which will be generated.

--update, -u

If specified, database will be rewrite. If not, new records will be added.

-f --file

Path to configuration .ini file related of project base directory. See configuration from file

Use generators as functions.

django-factory-boy-generator provides 3 generators:

  • generate_to_dict(factory_class)

Converting a factory’s output to a dict, including SubFactories.

  • generate_to_json(factory_class, quantity=1, **kwargs)

Generate json data based on factory class.Return list of dictionaries with generated data.

  • generate_to_db(factory_class, quantity=1, **kwargs)

Generate sample data and use it to fill database.

You also can use generators, for example, in unit tests:

from django.test import TestCase
from factory_generator import generate_to_db, generate_to_db
from sample_app.factories import PersonFactory


class TestSomeActions(TestCase):

    def setUp(self):
        self.persons = generate_to_db(PersonFactory, quantity=5)
        self.persons_data = generate_to_json(PersonFactory, quantity=5)

    ...  # your tests

Configuration from file

Instead of to pass options every time into command line, you can create .ini file with options, which will be used every time when you run commands:

[factory_generator]
labels=sample_app, another_app.SomeFactory
exclude=another_app.DontGenerateFactory
quantity=3
update=on

Advanced usage

By default, generate_to_json command uses custom DjangoFileJsonEncoder which extends DjangoJSONEncoder and serizlize file-objects to string of path to file. If you want to use another json encoder you can create your custon command extends factory_generator.management.commands.generate_to_json.Command and specify encoder pass keyword argument cls like this:

# your_app.management.commands.create_json

from factory_generator.management.commands.generate_to_json import Command as JsonCommand
from your_app.serializers import CustomJsonEncoder

class Command(JsonCommand):

    def generate(self, generate_factories, quantity=1, **kwargs)
        return super().generate(generate_factories, quantity=1, cls=CustomJsonEncoder)

Metadata

Release files for django-factory-boy-generator 1.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-factory-boy-generator 1.0.4
File Size Uploaded
django-factory-boy-generator-1.0.4.tar.gz 9.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-factory-boy-generator 1.0.4
File Interpreter ABI Platform
django_factory_boy_generator-1.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 19.8 kB

Release files / django-factory-boy-generator-1.0.4.tar.gz

Download URL django-factory-boy-generator-1.0.4.tar.gz
Size 9.1 kB
Tags Source
SHA-256 checksum
How to use checksums
95a2fe5a8a0c2964d98cd463b797cffd423f7839a972d061b74ce3c9522ed294
BLAKE2b-256 checksum
How to use checksums
1ba0c1868f6f760d533ccfe4b9cf906f088f7af6aa7ff4f1828919eb26350b56
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.2.0 pkginfo/1.5.0.1 requests/2.23.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3+

Release files / django_factory_boy_generator-1.0.4-py3-none-any.whl

Download URL django_factory_boy_generator-1.0.4-py3-none-any.whl
Size 10.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0a2c2f54a9587855eab02663442012d2ec028457a7ec21a5dae1b5888628000f
BLAKE2b-256 checksum
How to use checksums
dfc6e9994185a77cda496fbed87b40c5dde3d2b8ddadbb53bbf2b2a71bc0dad4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.2.0 pkginfo/1.5.0.1 requests/2.23.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3+

Release history Release notifications | RSS feed

This release

1.0.4 This release

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page