Django-seed uses the faker library to generate test data for your Django models. This has been "hard-forked" from django_faker in order to support newer versions of Python and Django
Django-seed allows you to write code to generate models, and seed your database with one simple manage.py
command!
To install django-seed, use pip:
pip install django-seed
Or to install from source:
python setup.py install
Add it to your installed apps in settings.py
:
INSTALLED_APPS = (
...
'django_seed',
)
Note: When seeding models with Foreign Keys, you need to make sure that those models are seeded first. For example, if a model in app A has a foreign key to a model in app B, you must seed app B first.
With django-seed, you can seed your database with test data from the command line using the manage.py seed
command.
Ex: Seed 15 of each model for the app api
:
That's it! Now you have 15 of each model seeded into your database.
Should you need, you can also specify what value a particular field should have. For example, if you want to seed 15 of MyModel
, but you need my_field
to be the same on all of them, you can do it like this:
This is the command equivalent to doing it in Python:
django-seed provides methods to easily seed test databases for your Django models. To seed your database with Model instances, import Seed
, get a seeder
instance, and use the add_entity method.
Ex: seeding 5 Game
and 10 Player
objects:
The seeder uses the name and column type to populate the Model with relevant data. If django-seed misinterprets a column name or column type and AttributeError(field) is thrown, you can still specify a custom function to be used for populating a particular column, by adding a third argument to the add_entity()
method:
Django-seed does not populate auto-incremented primary keys, instead seeder.execute()
returns the list of inserted PKs, indexed by class:
You may specify a different locale by passing it in the constructor of the seeder. Defaults to settings.LANGUAGE_CODE
Seed.seeder()
can take a locale as an argument, to return localized data. You can find all possible locales in faker's documentation
In order to apply localization, do the next:
To run django tests in a django environment, first make sure you have the packages from requirement-test.txt installed, then run the following:
or if you have django_seed
in INSTALLED_APPS:
MIT. See LICENSE for more details.