Skip to main content

This package provides django app to generate data for django apps using factories.

Project description

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)

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-factory-boy-generator-1.0.4.tar.gz (9.1 kB view hashes)

Uploaded Source

Built Distribution

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