Lab 7: Object-Relational Mapping with Django
This lab is designed to help you practice using Django’s object-relational mapping (ORM) functionality.
You may work with your project team.
Instructions
Based on the Django MiniFacebook example, you will implement Django models for the university database that you saw in Lab 5 and Homework 7.
Your goal is to implement these models and to connect them to the Django admin interface, so you can use it to create, read, update, and delete (basic CRUD operations) students, rooms, courses, and enrollments.
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 teammate’s computers.
Note: If you are using Windows, you should first follow these instructions to configure Git to use UNIX-style line endings. Otherwise, volume-mapped scripts will not run properly in the Docker containers. As also described in those instructions, you should use Git to clone the repo, in order to preserve filesystem permissions.
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
- Room
- Course
- Enrollment
You may find it helpful to refer to the documentation for Django models and Django model fields.
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
If you worked with your team, only one of you should upload the PDF to Moodle. You should submit a PDF containing:
- The names of your teammates
- Your copy-pasted
university/models.py
code - Screenshots of data in the admin interface for:
- Students
- Rooms
- Courses
- Enrollments
The assignment will be graded as part of your assignment grade.