Package: django-factory-boy-generator
App to generate data for django apps usingfactory-boyfactories.Table of contentsRequirementsInstallationUsageConfiguration from fileAdvanced usegeInstallationpip install django-factory-boy-generatorAddfactory_generatorto yourINSTALLED_APPSsetting.INSTALLED_APPS = [
    ...
    'factory_generator',
]RequirementsPython 3.6 or newerDjango >= 1.11 or newerfactory-boy >= 3.2 or newerUsageCreate in your django app(s) modulefactories.py.Define in this module factories according tofactory-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 EXCLUDEPrevents 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 QUANTITYQuantity of objects every factory which will be generated.--update, -uIf specified, database will be rewrite. If not, new records will be added.-f --filePath to configuration.inifile related of project base directory. Seeconfiguration from fileUse generators as functions.django-factory-boy-generatorprovides 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 testsConfiguration from fileInstead of to pass options every time into command line, you can create.inifile 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=onAdvanced usageBy default,generate_to_jsoncommand uses customDjangoFileJsonEncoderwhich extendsDjangoJSONEncoderand serizlize file-objects to string of path to file. If you want to use another json encoder you can create your custon command extendsfactory_generator.management.commands.generate_to_json.Commandand specify encoder pass keyword argumentclslike 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)