Django specific Extensions for Factory Boy
Project description
FactoryMan
Factory-Man provides Django specific extensions for Factory Boy.
Installation
pip install factory-man
Usage
FactoryMan provides you with a factory function create_populated_modelfactory
, which accepts the model class and automatically creates a factory class based on the model's fields. If you want to override some fields, you can pass them as keyword arguments to the __init__
method.
from factoryman import create_populated_modelfactory as cpm
from .models import Project
ProjectFactory = cpm(Project)
ExpiredProjectFactory = cpm(Project, deadline='1999-04-04')
To override the factory's _create
method, use create_override
keyword argument. This is useful to create many-to-many connections as described in the Factory Boy documentation.
Check out this article for a more detailed user guide.
Low-level Features
ModelFieldSequence
extends factory.Sequence
to provide a little more DRY syntax. It takes in a string
parameter and uses it to create a unique value for each object by adding -n
to the string, where n
is the count of objects created.
When working with Django, ModelFieldSequence
can also accept a model
parameter instead of string
. The model
should be a Django model. The name of the model and the field to which ModelFieldSequence
is signed to are used to automatically create the string
. model
can also be used as a positional argument.
Example:
from factory.django import DjangoModelFactory as ModelFactory
from factoryman import ModelFieldSequence
class CharityFactory(ModelFactory):
class Meta:
model = Charity
name = ModelFieldSequence(Charity) # Will be `Charity__name-n`, where n is the object count
email = ModelFieldSequence(string='hello@charity.ee') # Will be `hello@charity.ee-n`, where n is the object count
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
Built Distribution
Hashes for factory_man-1.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9b234af8bc38c474a343285b0792f1560915e0ddf935cf2438442f2c162dc05c |
|
MD5 | ab81dd80fa2cb06b3564a18b85f6219b |
|
BLAKE2b-256 | 97af100f38f9d957800e83901263b47c6a5bd438b29e800878b8c42fc535a319 |