Skip to main content

Generate Model Factory (factory_boy) for each model of your Django app

Project description

django-factory-generator v1.0.4 on PyPi MIT license Stable


Generate (factory_boy) Model Factory for each model of your Django app


  • 1.0.4 fix has_choices property for Django 3.0

  • 1.0.3 Remove useless print statements

  • 1.0.2 Add PointFieldFaker to support PointField. Refactor the way modules are imported on generated factories

  • 1.0.1 Add more accurate fakers for BigIntegerField, IntegerField, PositiveIntegerField, PositiveSmallIntegerField and SmallIntegerField

  • 1.0.0 Init project


This Django app generates factory_boy Model Factories from your installed apps, so you need to have factory_boy installed.


Install using pip :

pip install django_factory_generator

Add factory_generator to your INSTALLED_APPS settings.


Generate factories

Generate factories with a single command line:

python generate_factories

This will create a model_factories directory with the following structure :

|__ model_factories/
    |__ app_label_foo/
        |__ base/

Each model results in two generated files :

  • model_factories/app_label_foo/base/model_foo containing the ModelFooFactoryBase class definition generated from the model ModelFoo. This file should not be manually edited since it would be overriden each time the command generate_factories is run.

  • model_factories/app_label_foo/model_foo containing the ModelFooFactory class which simply extends ModelFooFactoryBase. This file is generated once but not overriden when you run the generate_factories command again.

This structure gives you the ability to override the ModelFactory that was automatically generated. You can then edit the ModelFooFactory to change / edit the base fields that were generated.

You can then import your model factories the following way:

# app_label/
from django.test import TestCase

from model_factories.app_label import ModelFooFactory, ModelBarFactory

class FooTests(TestCase):

    def test_model_factory(self):
        modelfoo = ModelFooFactory(
        modelbar = ModelBarFactory(
        # Run your tests here


Here are all the settings you can use, with their default value :

FACTORY_ROOT_DIR = 'model_factories'


  • Improve documentation

  • Write unit tests

  • validate compatibility with previous versions of Django and Python


If you are having issues, please let us know or submit a pull request.


The project is licensed under the MIT License.

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_generator-1.0.4.tar.gz (9.6 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