Django code snippets

Django code snippets

Creating fixtures for initial set of data

If the data is simple and static, you can use django fixtures to load the initial set of data.

Usage instructions:

  1. Create a folder "fixtures" in your project.
  2. Add FIXTURES_DIRS and map it to the folder created above. It accepts a list of paths.
  3. Use dumpdata and loaddata to load or save the data.
# Dump all tables of auth app into auth.json with indent of 4
# Default output format is json
python manage.py dumpdata --indent 4 auth > auth.json

# Dump customuser table of accounts into users.json with indent of 4
python manage.py dumpdata --indent 4 accounts.customer > users.json

# Load the fixture users.json
python manage.py loaddata users.json