Lab 4 Models
Lab 4 Models
PART 1: Edit models.py to update existing models and create new models
from django.db import models
import datetime
from django.contrib.auth.models import User
from django.utils import timezone
3. Update db tables.
a. Add a new model Order with fields:
books (ManyToManyField(Book))
o indicates the books that were part of this order.
member (ForeignKey(Member))
o indicates the member that placed the order
order_type (IntegerField)
o choices of valid values = {0,1}. The default value is 1. The values indicate the type of
order and are interpreted as: [(0,'Purchase'),(1, 'Borrow')]. HINT: Use
similar format as status field in Member model.
order_date(DateField)
o indicates date order was placed. The default value is current date (i.e. timezone.now)
b. Add a new required field country to Publisher model, with a default value of ‘USA’. This indicates
the country where the headquarters of the publisher is located.
COMP 8347: Internet Applications and Distributed Systems
SUMMER 2024 LAB #4
c. Add a new ‘optional’ field description to Book model. This provides a description of the book. The
field should be of type TextField.
d. Make the field address in Member model ‘optional’. This field indicates the physical address of the
member.
e. Set the default value of city field in Member model to ‘Windsor’.
Run makemigrations, sqlmigrate and migrate again until there are no errors. What is the latest file in
migrations dir? Open it and check its contents.
2. Start your server (Run Run ‘mysiteS24’) and navigate to admin site (127.0.0.1:8000/admin).
3. Login using superuser name and password (from Lab #3).
4. Enter the information for each publisher, book, member and order as given in lab4dataS24.txt
through the admin interface. Note: Some data was already entered in Lab 3.
How is the data being displayed? Would it be more useful to display additional information?
5. a. Write __str__ methods for each model.
b. For the Order model, write a method total_items(self) that returns the total books in the order.
6. In Python console import Django then models from models.py, then write queries to obtain the
following information. Verify if your query generates the correct answer using lab4dataS24.txt.
import django
from myapp.models import Publisher, Book, Member, Order
a. List all the books in the db.
b. List all the members in the db.
c. List all the orders in the db.
PART 3: Practice writing queries
1. Write queries to do the following.
a. List all Members whose last name is ‘Jones’
COMP 8347: Internet Applications and Distributed Systems
SUMMER 2024 LAB #4