Skip to main content

Django Simple Factory

A simple factory implementation built upon Faker.

Installation

pip install django-simple-factory

Usage

To use factories, create a factories.py inside of a Django app. Then subclass the django_simple_factory.factories.Factory class. To define the model generation, override the definition method.

from django_simple_factory.factories import Factory
from posts import models

class PostFactory(Factory):
    model = models.Post

    def definition(self):
        return {
            "title": self.faker.word(),
            "content": self.faker.text(),
        }

class CommentFactory(Factory):
    model = "comments.Comment" # You can provide a model string

    def definition(self):
        return {
            "content": self.faker.text(),
            "post": PostFactory() # You can provide either a factory instance or a factory string (e.g)
        }

To use these models, instaniate the factories, then call either make or create.

post_factory = PostFactory()
post_factory.make() # Does not persist
post_factory.create() # Does Persist

Factories that have related factories, say a Comment requires a Post, can pass a new dictionary for the post values to override.

comment_factory = CommentFactory()
comment = comment_factory.make(content="Hello", post={"title": "My Post"})

comment.content # Hello
comment.post.title # My Post

Alternatively, you may use the "django-style" of post__title="My Title".

comment_factory = CommentFactory()
comment = comment_factory.make(content="Hello", post__title="My Post")

comment.content # Hello
comment.post.title # My Post

To create related models (those that have a foreign key to the curren model) use the has method. Use the related_name to specify. You can optionally provide arguments and an amount to generate.

post_factory = PostFactory()
post = post_factory.has("comments").create() # Creates a Post with a related comment.

To enable easier testing, a FactoryTestMixin has been included that enables rich definition of factories.

from django_simple_factory.mixins import FactoryTestMixin
from django.test import TestCase
from posts import models

class PostTest(FactoryTestMixin, TestCase):
    factories = ["posts.PostFactory"]

    def test_post_creation(self):
        post = self.factories[models.Post].create()
        ...

API

django_simple_factory.factories.Factory

The primary class of the package, factories allow for simple and easy testing of Django models.

Method Description
configure_faker A hook that enables the configuration of the factory's faker instance. Each factory contains its own separate faker instance.
get_factory A class method that enables the discovery of a Factory (e.g. Factory.get_factory("posts.PostFactory")). The string passed in mimics the models access. To access a factory via string it should be of the form <app_name>.<factory_name>.
make Make is one of the primary methods for creating an model. make does not persist the model, rather it just gives an instance. To override the generated values, provide keyword arguments to the method. If you want to override related object creation, provide a keyword argument with a dictionary (e.g. post_factory.make(user={"email": "test@example.com"}))
create Create is very much like make, except it does persist the model.

Contact

If there are any issues, please feel free to make an issue. If you have suggested improvements, please make an issue where we can discuss.

Metadata

Release files for django-simple-factory 0.3.3

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-simple-factory 0.3.3
File Size Uploaded
django_simple_factory-0.3.3.tar.gz 21.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-simple-factory 0.3.3
File Interpreter ABI Platform
django_simple_factory-0.3.3-py3-none-any.whl Python 3 none any Details

Total release size: 30.8 kB

Release files / django_simple_factory-0.3.3.tar.gz

Download URL django_simple_factory-0.3.3.tar.gz
Size 21.3 kB
Tags Source
SHA-256 checksum
How to use checksums
86622ec0a46090985cb0533af524a26d31df84fca4600d0a202b9459aec19ea6
BLAKE2b-256 checksum
How to use checksums
d84bd94f87594825aca2235f4de7c451925f6f16fe95ca667b622556461d41a0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.32.4

Release files / django_simple_factory-0.3.3-py3-none-any.whl

Download URL django_simple_factory-0.3.3-py3-none-any.whl
Size 9.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dd5ba4fb5c0841ad208f1fc36f9b55e503e64e0360050c273d35a935fe54d2bc
BLAKE2b-256 checksum
How to use checksums
c28d46e55020f0446ce92342fe56f7244d9e960cca86254ad5c8a9737da8a238
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.32.4

Release history Release notifications | RSS feed

This release

0.3.3 This release

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

0.0.2

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