StaffCoder
Django
DjangoDjango 5.2Python 3.13
18 modules · 72 challengesSign up
Module 1 · Models & Relationships

Course Enrollment Links

easy83%5 of 6 solved

Problem statement

Students can take many courses, courses have many students, and each student has at most one transcript. app/models.py defines the three models, but nothing links them yet. Your task is to add the missing relationships, choosing the right relationship type for each.

Existing Models

app/models.py already defines all three models:

ModelFieldMeaning
Coursecodeshort text, at most 20 characters, never shared
Coursetitletext, at most 200 characters
Studentnametext, at most 200 characters
Studentemailan email address, never shared
Transcriptgpaa grade point average such as 3.50 (two decimal places)

Nothing connects them yet. The two links are what you add, at the # Write your code here markers.

Requirements

  1. Enrollment: many students per course, many courses per student. Add a courses field to Student (declared on Student, not on Course). A student can be enrolled in many courses and a course can hold many students. Enrolling a pair links them on both sides; removing an enrollment unlinks them on both sides.
  2. Enrollment is readable from either side. student.courses.all() returns the student's courses and course.students.all() returns the course's students. The reverse name students must belong to the relationship itself (a hand-written property does not count), and the Django default course.student_set must not exist.
  3. At most one transcript per student, enforced by the database. Add a student field to Transcript. A second transcript for the same student must be rejected with an IntegrityError. Deleting a student deletes their transcript.
  4. The transcript is a single object from either side. transcript.student returns the owning student, and student.transcript returns that student's transcript itself (an object with a gpa), not a collection of transcripts. Accessing it on a student who has none raises Student.transcript.RelatedObjectDoesNotExist.
Sample Usage
>>> alice = Student.objects.create(name="Alice", email="alice@example.com")
>>> algebra = Course.objects.create(code="MATH101", title="Algebra")
>>> alice.courses.add(algebra)
>>> list(algebra.students.values_list("name", flat=True))
['Alice']
>>> alice.courses.remove(algebra)
>>> list(algebra.students.all())
[]
>>> alice.transcript
Traceback (most recent call last):
    ...
app.models.Student.transcript.RelatedObjectDoesNotExist: Student has no transcript.
>>> Transcript.objects.create(student=alice, gpa=Decimal("3.50"))
>>> Student.objects.get(pk=alice.pk).transcript.gpa
Decimal('3.50')
>>> Transcript.objects.create(student=alice, gpa=Decimal("2.00"))
Traceback (most recent call last):
    ...
django.db.utils.IntegrityError: UNIQUE constraint failed: app_transcript.student_id

More in Models & Relationships

See the module
Product Model DefinitionProducts lose data: prices come back wrong, names have no length limit, and nothing records when an item was added. Define a Product model with a bounded name, an exact decimal price, a stock count with a sensible default, an active flag, an auto-set creation time and a readable string form.43%15 of 35 solvedeasyAuthor Book Foreign KeyBooks and authors live in unrelated tables, so deleting an author leaves orphaned books. Relate Book to Author so an author's books go with the author and are listed through the author, and add an optional publisher that cannot be deleted while it still has books.100%9 of 9 solvedeasyLibrary Domain SchemaModel a library from scratch: members, books, authors, loans and member cards. Use every relationship kind: books to authors (many-to-many), loans that link a member and a book and carry their own due date, and one card per member, with delete rules and readable string forms throughout.56%5 of 9 solvedeasy

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.