Fixtures and database - Mines-Paristech-Students/Portail-des-eleves GitHub Wiki
To develop and test your features locally, you need to set up a local database that will power the website. You should already have a Postgres database running (if not, read this first).
We will now take a look at how Django interacts with the database, and how to use Django fixtures to populate the database with fake data, which is very useful for testing a feature.
Django automatically manages the structural database changes (typically when a model is edited) using files called migrations. A migration can be generated by Django and then has to be applied to the database. For instance, after editing some models, one would run:
python manage.py makemigrations <app>
python manage.py migrate
The first command will create a file in polls/migrations
that contains the code to apply the changes to the database structure.
The second one applies the changes to the database.
This mechanism is very useful, since it guarantees traceability and ensures that the database can be upgraded to work with the new version of the software.
The migration files should be shared with everyone via version control.
After fetching the latest modifications from Github, you should always run the following command to update your local database:
python manage.py migrate
Every app should come with tests and fixtures. Let's focus on the latest.
Fixtures are initial data that populate the database before the app is used. That way, developers can check if the interface is relevant for instance (which is not easy to do with empty screens).
Fixtures are written in the /fixtures
folder of each application. To add fixtures, just put a JSON or a YAML file inside with the data you want to put in the database. For more information: https://docs.djangoproject.com/fr/2.1/howto/initial-data/
In order to allow people to easily use every account in dev mode, the password should be the same for everyone.
Let's choose something easy to remember: password
.
In the fixtures (as well as in the database) one doesn't store plain passwords but always the hash, so here it is :
pbkdf2_sha256$100000$tos6gO0V3tNL$Vd14vNq3N5MwGX6TsvBV0RW+DQzGpy3OGfKqCtL3kls=
You may need to populate the initially empty database with the fixtures, or to reset it at some point.
On Unix you can run the script cli/create_and_populate_database.bash
(cli/create_band_populate_database.bat
if you use Windows).
If you prefer to do it manually, the following commands may be useful:
python manage.py reset_db
-
python manage.py makemigrations <app>
andpython manage.py migrate
python manage.py loaddata <app>