Django Models

Our next step is to add some models to our LocalLibrary application so we can store data in our database.

You can view a video walkthrough of this assignment.

Read and understand the tutorial, and when you get to the section titled Defining the LocalLibrary Models, go to your PythonAnywhere account and start a bash shell.

Lets go into the locallibrary project:

workon django2
cd ~/django_projects/locallibrary

Make sure that you have properly edited the locallibrary/ file to register your catalog application within the locallibrary project by adding CatalogConfig line:

ALLOWED_HOSTS = ['*']   <-- Change

    'django_extensions',             # Add this line
    'catalog.apps.CatalogConfig',    # Add this line

Edit the file catalog/ and add the Genre model using as described in the tutorial. You can edit the file with nano, vi, or the PythonAnywhere Files tab interface. Once you have added the model, run the migrations from ~/django_projects/locallibrary

python3 makemigrations
python3 migrate

If you are using git, you can see what files have been modified / created:

git status

The git output would be as follows:

modified:   catalog/
Untracked files:

The git command won't show the db.sqlite3 file has changed because we have told git not to track the database file in the .gitignore file

Lets take a quick look at the contents of the db.sqlite3 file in your bash shell:

$ sqlite3 db.sqlite3 
SQLite version 3.11.0 2016-02-15 17:29:24
Enter ".help" for usage hints.
sqlite> .tables
auth_group                  catalog_genre
auth_group_permissions      django_admin_log
auth_permission             django_content_type
auth_user                   django_migrations
auth_user_groups            django_session
sqlite> .mode column
sqlite> select * from catalog_genre;
sqlite> .schema catalog_genre
CREATE TABLE "catalog_genre" (
    "name" varchar(200) NOT NULL);
sqlite> .quit  

You can learn more about the command line mode of sqlite3 at their web site:

Continue editing the catalog/ file and add the Book, BookInstance, and Author models according to the tutorial.

Also add the language field to the correct table as discussed in the "Challenge" section at the end of the tutorial.

Once your file is complete, run the migrations again:

cd ~/django_projects/locallibrary
python3 makemigrations
python3 migrate

You can repeat the process of editing the file and re-running the migrations until you get them right.

If you are using the autograder for this assignment, you will need to upload the db.sqlite3 file. If you are using PythonAnywhere you can use the Files tab to download the file to your computer and then upload it to the autograder.