Lab: Object-Relational Mapping with Django
In this lab, you will practice using Django’s object-relational mapping (ORM) functionality.
Instructions
You should work on this assignment either individually or with a partner.
Based on the Django MiniFacebook example, you will implement Django models for the university database that you used in lab and homework.
Note: Windows users: if you haven’t already, complete Step 2 from my guide to Configuring Windows for Docker Development.
Step 1: Get MiniFacebook Running
Follow the instructions in the the csci220-django repo to get the Django MiniFacebook example running on each of your teammates’ computers.
Step 2: Implement Models
Based on the MiniFacebook examples in django/djangoproject/minifacebook/models.py
, you should edit django/djangoproject/university/models.py
to implement Django models for:
- Student
- Course
- Room
- Enrollment
You should implement the attributes shown in the csci220-uwsgi repo’s university.sql
.
You should refer to the documentation for Django models and Django model fields.
Hint: Consider using a ManyToManyField
to represent enrollment.
Step 3: Generate and Apply Migrations
When you edit Django models, the changes don’t take effect until you update the database. This is done in two steps: creating a migration file, and then running it.
Follow the instructions in the the csci220-django repo to create and run a migration file.
Step 4: Access the Admin Interface
Follow the instructions in the the csci220-django repo to access the Django admin interface.
You won’t see the university models yet, since you haven’t yet connected them to the admin interface.
Step 5: Register University Models with Admin Interface
Based on the MiniFacebook examples in django/djangoproject/minifacebook/admin.py
, you should edit django/djangoproject/university/admin.py
to register your models with the admin interface.
After restarting the django
container, you should see your models in the admin interface!
Step 6: Add Records to the Database
Use the admin interface to add students, rooms, courses, and enrollments.
Try entering incorrect data, and see how the admin interface handles this.
Submit
Upload:
- Your
university/models.py
code - Screenshots of data in the admin interface for the university models (i.e., Student, Course, Room, Enrollment)
Only one partner should submit. This lab will count towards your attendance grade.