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

Author Book Foreign Key

easy100%9 of 9 solved

Problem statement

Books, authors and publishers are stored in unrelated tables, so nothing connects a book to who wrote it or who published it. Your task is to relate the models in app/models.py, choosing the relationship type and what happens on delete for each link.

Existing Models

app/models.py already defines three models:

ModelFieldMeaning
Authornametext, at most 200 characters
Publishernametext, at most 200 characters
Booktitletext, at most 200 characters

Book has no link to Author or Publisher yet. Those links are what you add, at the # Write your code here marker in Book.

Requirements

  1. Every book has exactly one author; an author can have many books. Add an author field to Book. It is required: saving a Book without an author must be rejected (by the database or by Django).
  2. An author's books go with the author. Deleting an Author deletes that author's books too, and leaves other authors' books untouched.
  3. An author's books are reachable as author.books. author.books.all() returns that author's books, and the relation must also work in queries that go from author to book, for example Author.objects.filter(books__title="The Hobbit"). The Django default name (author.book_set) must not exist. A hand-written books property does not count: the name has to belong to the relationship itself.
  4. A book may have a publisher, and a publisher with books cannot be deleted. Add a publisher field to Book. It is optional: a book with no publisher (publisher=None) saves fine. Each book has at most one publisher; a publisher can have many books. Deleting a Publisher that still has at least one book must be refused with Django's ProtectedError, and the publisher and its books must stay in the database. A publisher with no books deletes normally.
Sample Usage
>>> rowling = Author.objects.create(name="J. K. Rowling")
>>> stone = Book.objects.create(title="Philosopher's Stone", author=rowling)
>>> stone.author.name
'J. K. Rowling'
>>> list(rowling.books.values_list("title", flat=True))
["Philosopher's Stone"]
>>> list(Author.objects.filter(books__title="Philosopher's Stone"))
[<Author: J. K. Rowling>]
>>> rowling.delete()
>>> Book.objects.count()
0

>>> penguin = Publisher.objects.create(name="Penguin") >>> tolkien = Author.objects.create(name="J. R. R. Tolkien") >>> Book.objects.create(title="The Hobbit", author=tolkien, publisher=penguin) <Book: The Hobbit> >>> penguin.delete() django.db.models.deletion.ProtectedError: ...

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 solvedeasyCourse Enrollment LinksStudents take many courses, courses have many students, and each student has exactly one transcript. Model the many-to-many and the one-to-one, readable from both sides, with at most one transcript per student enforced by the database.83%5 of 6 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.