A model instances generator for Django
Project description
Django-fakery
=============
.. image:: https://travis-ci.org/fcurella/django-fakery.svg?branch=master
:target: https://travis-ci.org/fcurella/django-fakery
.. image:: https://coveralls.io/repos/fcurella/django-fakery/badge.svg?branch=master&service=github
:target: https://coveralls.io/github/fcurella/django-fakery?branch=master
An easy-to-use implementation of `Creation Methods`_ (aka Object Factory) for Django, backed by ``Faker``.
.. _Creation Methods: http://xunitpatterns.com/Creation%20Method.html
``django_fakery`` will try to guess the field's value based on the field's name and type.
Installation
------------
Install with::
$ pip install django-fakery
QuickStart
----------
.. code-block:: python
from django_fakery import factory
factory.make(
'app.Model',
fields={
'field': 'value',
}
)
The value of a field can be any python object, a callable, or a lambda:
.. code-block:: python
from django_fakery import factory
from django.utils import timezone
factory.make(
'app.Model',
fields={
'created': timezone.now
}
)
When using a lambda, it will receive two arguments: ``n`` is the iteration number, and ``f`` is an instance of ``faker``:
.. code-block:: python
user = factory.make(
'auth.User',
fields={
'username': lambda n, f: 'user_{}'.format(n),
}
)
You can create multiple objects by using the ``quantity`` parameter:
.. code-block:: python
from django_fakery import factory
factory.make('app.Model', quantity=4)
For convenience, when the value of a field is a string, it will be interpolated with the iteration number:
.. code-block:: python
user = factory.make(
'auth.User',
fields={
'username': 'user_{}',
},
quantity=4
)
Lazies
------
You can refer to the created instance's own attributes or method by using `Lazy` objects.
For example, if you'd like to create user with email as username, and have them always match, you could do:
.. code-block:: python
from django_fakery import factory, Lazy
factory.make(
'auth.User',
fields={
'username': Lazy('email'),
}
)
If you want to assign a value returned by a method on the instance, you can pass the method's arguments to the ``Lazy`` object:
.. code-block:: python
from django_fakery import factory, Lazy
factory.make(
'myapp.Model',
fields={
'myfield': Lazy('model_method', 'argument', keyword='keyword value'),
}
)
Foreign keys
------------
Non-nullable ``ForeignKey``s create related objects automatically.
If you want to explicitly create a related object, you can pass it to the ``fields`` like any other value:
.. code-block:: python
pizza = factory.make(
'food.Pizza',
fields={
'chef': factory.make('auth.User', fields={'username': 'Gusteau'}),
}
)
ManyToManies
------------
Because ``ManyToManyField``s are implicitly nullable (ie: they're always allowed to have their ``.count()`` equal to ``0``), related objects on those fields are not automatically created for you.
If you want to explicitly create a related objects, you can pass a list to the ``fields`` like any other value:
.. code-block:: python
pizza = factory.make(
'food.Pizza',
fields={
'toppings': [factory.make('food.Tooping', fields={'name': 'Anchovies'})],
}
)
You can also pass a factory, to create multiple objects:
.. code-block:: python
pizza = factory.make(
'food.Pizza',
fields={
'toppings': factory.make('food.Tooping', quantity=5),
}
)
Lazies
------
You can refer to the created instance's own attributes or method by using `Lazy` objects.
For example, if you'd like to create user with email as username, and have them always match, you could do:
.. code-block:: python
from django_fakery import factory, Lazy
factory.make(
'auth.User',
fields={
'username': Lazy('email'),
}
)
If you want to assign a value returned by a method on the instance, you can pass the method's arguments to the ``Lazy`` object:
.. code-block:: python
from django_fakery import factory, Lazy
factory.make(
'myapp.Model',
fields={
'myfield': Lazy('model_method', 'argument', keyword='keyword value'),
}
)
Pre-save and Post-save hooks
----------------------------
You can define functions to be called right before the instance is saved or right after:
.. code-block:: python
from django_fakery import factory
factory.make(
'auth.User',
fields={
'username': 'username',
},
pre_save=[
lambda i: i.set_password('password')
]
)
Since settings a user's password is such a common case, we special-cased that scenario, so you can just pass it as a field:
.. code-block:: python
from django_fakery import factory
factory.make(
'auth.User',
fields={
'username': 'username',
'password': 'password',
}
)
Blueprints
----------
Use a blueprint:
.. code-block:: python
from django_fakery import factory
user = factory.blueprint('auth.User')
user.make(quantity=10)
Blueprints can refer other blueprints:
.. code-block:: python
pizza = factory.blueprint(
'food.Pizza',
fields={
'chef': user,
}
)
Seeding the faker
-----------------
.. code-block:: python
from django_fakery import factory
factory.make('auth.User', fields={
'username': 'regularuser_{}'
}, seed=1234, quantity=4)
Credits
-------
The API is heavily inspired by `model_mommy`_.
.. _model_mommy: https://github.com/vandersonmota/model_mommy
License
-------
This software is released under the MIT License.
=============
.. image:: https://travis-ci.org/fcurella/django-fakery.svg?branch=master
:target: https://travis-ci.org/fcurella/django-fakery
.. image:: https://coveralls.io/repos/fcurella/django-fakery/badge.svg?branch=master&service=github
:target: https://coveralls.io/github/fcurella/django-fakery?branch=master
An easy-to-use implementation of `Creation Methods`_ (aka Object Factory) for Django, backed by ``Faker``.
.. _Creation Methods: http://xunitpatterns.com/Creation%20Method.html
``django_fakery`` will try to guess the field's value based on the field's name and type.
Installation
------------
Install with::
$ pip install django-fakery
QuickStart
----------
.. code-block:: python
from django_fakery import factory
factory.make(
'app.Model',
fields={
'field': 'value',
}
)
The value of a field can be any python object, a callable, or a lambda:
.. code-block:: python
from django_fakery import factory
from django.utils import timezone
factory.make(
'app.Model',
fields={
'created': timezone.now
}
)
When using a lambda, it will receive two arguments: ``n`` is the iteration number, and ``f`` is an instance of ``faker``:
.. code-block:: python
user = factory.make(
'auth.User',
fields={
'username': lambda n, f: 'user_{}'.format(n),
}
)
You can create multiple objects by using the ``quantity`` parameter:
.. code-block:: python
from django_fakery import factory
factory.make('app.Model', quantity=4)
For convenience, when the value of a field is a string, it will be interpolated with the iteration number:
.. code-block:: python
user = factory.make(
'auth.User',
fields={
'username': 'user_{}',
},
quantity=4
)
Lazies
------
You can refer to the created instance's own attributes or method by using `Lazy` objects.
For example, if you'd like to create user with email as username, and have them always match, you could do:
.. code-block:: python
from django_fakery import factory, Lazy
factory.make(
'auth.User',
fields={
'username': Lazy('email'),
}
)
If you want to assign a value returned by a method on the instance, you can pass the method's arguments to the ``Lazy`` object:
.. code-block:: python
from django_fakery import factory, Lazy
factory.make(
'myapp.Model',
fields={
'myfield': Lazy('model_method', 'argument', keyword='keyword value'),
}
)
Foreign keys
------------
Non-nullable ``ForeignKey``s create related objects automatically.
If you want to explicitly create a related object, you can pass it to the ``fields`` like any other value:
.. code-block:: python
pizza = factory.make(
'food.Pizza',
fields={
'chef': factory.make('auth.User', fields={'username': 'Gusteau'}),
}
)
ManyToManies
------------
Because ``ManyToManyField``s are implicitly nullable (ie: they're always allowed to have their ``.count()`` equal to ``0``), related objects on those fields are not automatically created for you.
If you want to explicitly create a related objects, you can pass a list to the ``fields`` like any other value:
.. code-block:: python
pizza = factory.make(
'food.Pizza',
fields={
'toppings': [factory.make('food.Tooping', fields={'name': 'Anchovies'})],
}
)
You can also pass a factory, to create multiple objects:
.. code-block:: python
pizza = factory.make(
'food.Pizza',
fields={
'toppings': factory.make('food.Tooping', quantity=5),
}
)
Lazies
------
You can refer to the created instance's own attributes or method by using `Lazy` objects.
For example, if you'd like to create user with email as username, and have them always match, you could do:
.. code-block:: python
from django_fakery import factory, Lazy
factory.make(
'auth.User',
fields={
'username': Lazy('email'),
}
)
If you want to assign a value returned by a method on the instance, you can pass the method's arguments to the ``Lazy`` object:
.. code-block:: python
from django_fakery import factory, Lazy
factory.make(
'myapp.Model',
fields={
'myfield': Lazy('model_method', 'argument', keyword='keyword value'),
}
)
Pre-save and Post-save hooks
----------------------------
You can define functions to be called right before the instance is saved or right after:
.. code-block:: python
from django_fakery import factory
factory.make(
'auth.User',
fields={
'username': 'username',
},
pre_save=[
lambda i: i.set_password('password')
]
)
Since settings a user's password is such a common case, we special-cased that scenario, so you can just pass it as a field:
.. code-block:: python
from django_fakery import factory
factory.make(
'auth.User',
fields={
'username': 'username',
'password': 'password',
}
)
Blueprints
----------
Use a blueprint:
.. code-block:: python
from django_fakery import factory
user = factory.blueprint('auth.User')
user.make(quantity=10)
Blueprints can refer other blueprints:
.. code-block:: python
pizza = factory.blueprint(
'food.Pizza',
fields={
'chef': user,
}
)
Seeding the faker
-----------------
.. code-block:: python
from django_fakery import factory
factory.make('auth.User', fields={
'username': 'regularuser_{}'
}, seed=1234, quantity=4)
Credits
-------
The API is heavily inspired by `model_mommy`_.
.. _model_mommy: https://github.com/vandersonmota/model_mommy
License
-------
This software is released under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
django-fakery-1.1.0.tar.gz
(8.0 kB
view details)
Built Distribution
File details
Details for the file django-fakery-1.1.0.tar.gz
.
File metadata
- Download URL: django-fakery-1.1.0.tar.gz
- Upload date:
- Size: 8.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7a4151236f8e633da2f8244fa6445b1d9fa656085d408fcd805b872d59106f0f |
|
MD5 | 2404cd7f95223d0aae066b7bf6677ad8 |
|
BLAKE2b-256 | fe463524c60555a8e7b9d92fde63cf54de59f2b708d9162724999819c94c7c61 |
Provenance
File details
Details for the file django_fakery-1.1.0-py2.py3-none-any.whl
.
File metadata
- Download URL: django_fakery-1.1.0-py2.py3-none-any.whl
- Upload date:
- Size: 12.1 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e8f8ec768d51845bb845f6d0fb3d866f7dc90c5cfa6efb2cdaecbd9e26e3dbc3 |
|
MD5 | c9afe84c3054a066f844746f0a7df128 |
|
BLAKE2b-256 | 445905dbeff55d221c709521cfd0000475689ae2bb883c4c46b03608b2537b73 |