0% found this document useful (0 votes)
17 views3 pages

Lab 4 Models

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views3 pages

Lab 4 Models

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

COMP 8347: Internet Applications and Distributed Systems

SUMMER 2024 LAB #4

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

1. Add the following model.


a. Member with fields below:
class Member(User):
STATUS_CHOICES = [
(1, 'Regular member'),
(2, 'Premium Member'),
(3, 'Guest Member'),
]

status = models.IntegerField(choices=STATUS_CHOICES, default=1)


address = models.CharField(max_length=300)
city = models.CharField(max_length=20)
province=models.CharField(max_length=2, default='ON')
last_renewal = models.DateField(default=timezone.now)
auto_renew = models.BooleanField(default=True)
borrowed_books = models.ManyToManyField(Book, blank=True)

2. Create new db table. See what happens after each step.


a. Tools  Run manage.py Task… (opens a window where you can type manage.py commands)
b. In manage.py window: Type makemigrations myapp in dialog box.
c. Optional step: Check latest file in migrations dir (use sqlmigrate myapp **** in manage.py)
d. In manage.py window: Type migrate

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.

PART 2: Enter data through Admin interface


1. Update admin.py as follows:
from django.contrib import admin
from .models import Publisher, Book, Member, Order

# Register your models here.


admin.site.register(Publisher)
admin.site.register(Book)
admin.site.register(Member)
admin.site.register(Order)

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

b. List all Publishers with headquarters in ‘USA’


c. List all Members that live in ‘Windsor’.
d. List all Members that live on an ‘Avenue’ and live in AB province.
e. List all the Members that have borrowed the book 'A New World'
f. List the Books that cost more than $100.00
g. List the Members that do NOT live in province AB.
h. List the Orders placed by a client whose first_name is ‘Mary’.
i. List the Members whose member status are ‘Premium Member’.
j. List the Books that have between 100 and 280 pages (inclusive) and belong to category
‘Science&Tech’.
k. Get the first name of the Member of the Order with pk=1.
l. List all Books that the Member with username ‘bill’ is currently borrowing.
m. List all the Members who live in AB and have auto_renew enabled.
n. List the Books that ‘mary’ has purchased. (You may assume that ‘mary’ has exactly one purchase
order.)
o. List the city where the headquarters of the publisher of the book borrowed by ‘alan’ is located. You
may assume that the ‘alan’ is currently borrowing exactly one book.

You might also like