Populating the Bookr project’s database
Although we know how to create database records for the project, in the next few chapters, we will have to create a lot of records to work with the project. Therefore, we have created a script that can make things easy for us. This script populates the database by reading a comma-separated values (CSV) file consisting of many records. Follow the next few steps to populate the project’s database:
- Create the following folder structure inside the project directory:
bookr/reviews/management/commands/
- Copy the
loadcsv.py
file from the following location andWebDevWithDjangoData.csv
into the folder created. This can be found on the GitHub repository for this book at http://packt.live/3pvbCLM.
Because loadcsv.py
is placed inside the management/commands
folder, it now works like a Django custom management command. You can go through the loadcsv.py
file and read more about writing Django custom management commands here: https://docs.djangoproject.com/en/3.0/howto/custom-management-commands/.
- Now, let’s recreate a fresh database. Delete the SQL database file present in the project folder:
rm db.sqlite3
- To create a fresh database, execute the Django
migrate
command:python manage.py migrate
Now you can see the newly created db.sqlite3
file under the bookr
folder.
- Execute the
loadcsv
custom management command to populate the database:python manage.py loadcsv --csv reviews/management/commands/WebDevWithDjangoData.csv
- Using DB Browser for SQLite, verify that all the tables created by the
bookr
project have been populated.
With this, we have successfully populated our application’s database, which will come in handy to work with in the upcoming chapters of this book.