Seed your Django project with fake data
Project description
Fork from django-seed
Source’s documentation:
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!
Installation
To install django-seed, use pip:
pip install django-seeder
Or to install from source:
python setup.py install
Configuration
Add it to your installed apps in settings.py:
INSTALLED_APPS = ( ... 'django_seeder', )
Usage
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.
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.
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:
$ python manage.py seed api --number=15 --seeder "MyModel.my_field" "1.1.1.1"
This is the command equivalent to doing it in Python:
seeder.add_entity(MyModel, 10, {
'my_field': '1.1.1.1',
})
Using with code
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:
from django_seeder 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 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:
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 auto-incremented 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]
}
You may specify a different locale by passing it in the constructor of the seeder. Defaults to settings.LANGUAGE_CODE
seeder = Seed.seeder(locale='sv_SE')
seeder.faker.city() # 'Västerås'
Localization
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:
seeder = Seed.seeder('it_IT')
Tests
To run django tests in a django environment, first make sure you have the packages from requirement-test.txt installed, then run the following:
$ python runtests.py
or if you have django_seeder in INSTALLED_APPS:
$ python manage.py test django_seeder
License
MIT. See LICENSE for more details.
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
Built Distribution
File details
Details for the file django-seeder-0.1.0.tar.gz
.
File metadata
- Download URL: django-seeder-0.1.0.tar.gz
- Upload date:
- Size: 18.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1d883848587072bd74c7a67d92aee8870767dcdd5d85f289b364561130d18494 |
|
MD5 | 013258d0b95f60b9ec60813617b01c8b |
|
BLAKE2b-256 | 8b12ef359e684d3c1e63a95a36bf5b5dcc86c3ae54884871e83e85dd0243b1c7 |
File details
Details for the file django_seeder-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: django_seeder-0.1.0-py3-none-any.whl
- Upload date:
- Size: 17.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 237a0d9294a9c3c2ecec3aa0a9bdf2d9342613bbb78d16a26579213cc148ad84 |
|
MD5 | e3a339580978ddeaad021b41befb5f6e |
|
BLAKE2b-256 | 9ab858c76039331697c2b8f7ce44f5e82b84b34250417feba322f99c77e3ebf8 |