Skip to main content

Seed your Django project with fake data

Project description

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 version of Python and Django

Django-seed allows you to write code to generate models, and seed your database with one simple manage.py command!


Python 2.7, 3.x pypi Travis Build coverage MIT License



Installation

To install django-seed you can use pip:

pip install django-seed

Configuration

In django application settings.py:

INSTALLED_APPS = (
    ...
    'django_seed',
)

Usage

Using with command

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:

$ python manage.py seed api --number=15

That’s it! Now you have 15 of each model seeded into your database.

Using with code

django-seed provides methods to easily seed test databases for your Django models. To seed your database with Model instances, create a Seed instance and use the add_entity method.

Ex: seeding 5 Game and 10 Player objects:

from django_seed import Seed

seeder = Seed.seeder()

from myapp.models import Game, Player
seeder.add_entity(Game, 5)
seeder.add_entity(Player, 10)

inserted_pks = seeder.execute()

The seeder uses the name and column type to populate the Model with relevant data. If django-seed misinterprets a column name, you can still specify a custom function to be used for populating a particular column, by adding a third argument to add_entity() method:

seeder.add_entity(Player, 10, {
    'score':    lambda x: random.randint(0,1000),
    'nickname': lambda x: seeder.faker.email(),
})
seeder.execute()

Django-seed does not populate autoincremented primary keys, instead seeder.execute() returns the list of inserted PKs, indexed by class:

print inserted_pks
{
    <class 'faker.django.tests.Player'>: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    <class 'faker.django.tests.Game'>: [1, 2, 3, 4, 5]
}

Tests

Run django tests in a django environment:

$ python runtests.py

or if you have django_seed in INSTALLED_APPS:

$ python manage.py test django_seed

License

MIT. See LICENSE for more details.

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

django-seed-0.1.2.tar.gz (8.3 kB view hashes)

Uploaded Source

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