It’s sometimes useful to pre-populate your database with hard-coded data when you’re first setting up an app. You can provide initial data with migrations or fixtures.
Providing initial data with migrations¶
You can load data by calling manage.py loaddata <fixturename> , where <fixturename> is the name of the fixture file you’ve created. Each time you run loaddata , the data will be read from the fixture and re-loaded into the database. Note this means that if you change one of the rows created by a fixture and then run loaddata again, you’ll wipe out any changes you’ve made.
Providing data with fixtures¶
If you want to automatically load initial data for an app, create a data migration . Migrations are run when setting up the test database, so the data will be available there, subject to some limitations .
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
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:
Django-seed allows you to write code to generate models, and seed your database with one simple manage.py command!
In ruby on rails, I use seed.rb and then run “rake db:seed” in command line.
Main reason I want to seed some data on statuses, types, etc for the project initialization.
5 Answers 5
I’ved been developing in Django and was wondering if there is a way to seed data into the database in Django.
Is there something similar ?
Similar to Rails, we also have option to seed the database. It is done using management commands. In one of your apps, use the following folder structure