Problem statement
A library system needs its whole domain modeled from scratch: members, books, authors, loans, and member cards. app/models.py declares the five models as empty shells; your task is to give each one its fields, relationships, constraints and string representation. You choose the field types, the relationship types and what happens on delete; the requirements below describe the behaviour. Migrations are not graded. The tests build the database directly from your app/models.py (and generate the schema from it themselves), so you do not need to create or commit an app/migrations/ folder. The Run button runs makemigrations for you.
Domain Model
Five models live in app/models.py: Author, Member, MemberCard, Book, Loan. Each is currently an empty models.Model with a # Write your code here marker.
Author
| Field | Meaning |
|---|---|
| name | required text, at most 200 characters |
Member
| Field | Meaning |
|---|---|
| name | required text, at most 200 characters |
| joined_at | date and time filled in automatically on create, never changed after |
MemberCard: a member's library card
| Field | Meaning |
|---|---|
| member | the member who owns the card. A member has at most one card, reachable as member.card. |
| serial | text, at most 20 characters; no two cards may share a serial (enforced by the database) |
| is_active | true/false flag, True unless set otherwise |
Book
| Field | Meaning |
|---|---|
| title | required text, at most 200 characters |
| authors | the book's authors. A book can have many authors and an author many books; an author's books are author.books. |
| replacement_cost | exact money amount: at most 6 digits in total, exactly 2 after the decimal point; never negative |
| copies | whole number that can never be negative, 0 unless set otherwise |
| is_available | true/false flag, True unless set otherwise |
| added_at | date and time filled in automatically on create, never changed after |
Loan: one member borrowing one book
| Field | Meaning |
|---|---|
| member | the borrowing member (required). A member can have many loans, reachable as member.loans. |
| book | the borrowed book (required). A book can have many loans, reachable as book.loans. |
| due_date | required calendar date (no time of day) |
| returned | true/false flag, False unless set otherwise |
Requirements
- Every relation is readable from both sides by its name. author.books, member.card, member.loans and book.loans must be the reverse names of the relationships themselves, so they also work in queries such as Member.objects.filter(loans__book=book). A hand-written property with the same name does not count. The Django default names (author.book_set, member.loan_set, member.membercard) must not exist. member.card returns the card itself, and raises Member.card.RelatedObjectDoesNotExist for a member without one.
- Field rules. Text limits and the serial uniqueness are as in the tables. replacement_cost keeps exactly two decimal places, holds at most six digits in total, and a negative amount is invalid. Report that error on the replacement_cost field (a field validator), not as a model-wide error. Validation runs through full_clean(); save() does not call it. Defaults and automatic timestamps are as in the tables.
What happens on delete.
- Deleting a Member deletes that member's card and loans.
- Deleting a Book that still has loans is refused with Django's ProtectedError, and nothing is deleted; once its loans are gone, the book deletes normally.
- Deleting an Author removes the author's links to books, but the books themselves remain.
- A Loan cannot be saved without both a member and a book.
String representations.
Model str(obj) Example Author the author's name Ursula K. Le Guin Member the member's name Ada Lovelace MemberCard Card <serial> Card LIB-0001 Book <title> ($<replacement_cost>), cost always with two decimal places A Wizard of Earthsea ($15.00); Tehanu ($12.00) for Decimal("12") Loan <book title> -> <member name> (due <YYYY-MM-DD>) A Wizard of Earthsea -> Ada Lovelace (due 2026-03-01)
Sample Usage
>>> from datetime import date
>>> from decimal import Decimal
>>> leguin = Author.objects.create(name="Ursula K. Le Guin")
>>> earthsea = Book.objects.create(title="A Wizard of Earthsea", replacement_cost=Decimal("15.00"))
>>> earthsea.authors.add(leguin)
>>> list(leguin.books.values_list("title", flat=True))
['A Wizard of Earthsea']
>>> ada = Member.objects.create(name="Ada Lovelace")
>>> MemberCard.objects.create(member=ada, serial="LIB-0001")
<MemberCard: Card LIB-0001>
>>> ada.card.is_active
True
>>> loan = Loan.objects.create(member=ada, book=earthsea, due_date=date(2026, 3, 1))
>>> str(loan)
'A Wizard of Earthsea -> Ada Lovelace (due 2026-03-01)'
>>> earthsea.delete() # blocked: the loan still references it
django.db.models.deletion.ProtectedError: ...
>>> ada.delete() # cascades to the card and the loan
>>> earthsea.delete() # now allowed
