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.
Lets go into the locallibrary project:
workon django2 cd ~/django_projects/locallibrary
Make sure that you have properly edited the
locallibrary/settings.py file to register your catalog
application within the locallibrary project by adding
ALLOWED_HOSTS = ['*'] <-- Change INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'django_extensions', # Add this line 'catalog.apps.CatalogConfig', # Add this line ]
Edit the file
catalog/models.py and add the
Genre model using as described in the tutorial.
You can edit the file with
vi, or the PythonAnywhere Files tab interface. Once you have added
the model, run the migrations from
python3 manage.py makemigrations python3 manage.py migrate
If you are using
git, you can see what files have been modified / created:
The git output would be as follows:
modified: catalog/models.py Untracked files: catalog/migrations/0001_initial.py
git command won't show the
db.sqlite3 file has changed because we have told
not to track the database file in the
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 auth_user_user_permissions sqlite> .mode column sqlite> select * from catalog_genre; sqlite> .schema catalog_genre CREATE TABLE "catalog_genre" ( "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "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/models.py 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 models.py file is complete, run the migrations again:
cd ~/django_projects/locallibrary python3 manage.py makemigrations python3 manage.py migrate
You can repeat the process of editing the
models.py file and re-running the migrations until you get them
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.