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 details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_factory_boy_generator-1.0.4-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

Details for the file django-factory-boy-generator-1.0.4.tar.gz.

File metadata

  • Download URL: django-factory-boy-generator-1.0.4.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using 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+

File hashes

Hashes for django-factory-boy-generator-1.0.4.tar.gz
Algorithm Hash digest
SHA256 95a2fe5a8a0c2964d98cd463b797cffd423f7839a972d061b74ce3c9522ed294
MD5 90bc5b518ff4b14b6ae86cd5247d0d5f
BLAKE2b-256 1ba0c1868f6f760d533ccfe4b9cf906f088f7af6aa7ff4f1828919eb26350b56

See more details on using hashes here.

File details

Details for the file django_factory_boy_generator-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: django_factory_boy_generator-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 10.7 kB
  • Tags: Python 3
  • Uploaded using 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+

File hashes

Hashes for django_factory_boy_generator-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 0a2c2f54a9587855eab02663442012d2ec028457a7ec21a5dae1b5888628000f
MD5 020b3cb8e341611537c02bc567ff8e23
BLAKE2b-256 dfc6e9994185a77cda496fbed87b40c5dde3d2b8ddadbb53bbf2b2a71bc0dad4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page