I have the following in my models.py
:
class Size(models.Model):
size = models.CharField(max_length=20, primary_key=True)
class Book(models.Model):
title = models.CharField(max_length=100, primary_key=True)
size = models.ForeignKey(Size, null=False)
class Pamphlet(models.Model):
title = models.CharField(max_length=100, primary_key=True)
size = models.ForeignKey(Size, null=False)
book = models.ForeignKey(Book, null=True)
When the database is initially built the Size, Book, and Pamphlet tables are populated and each Book and Pamphlet is associated with a single size. However, the foreign key from Pamphlet to Book is not set.
It will be the responsibility of a user to go to the admin page and choose which book to associate with each pamphlet. Django's admin functionality makes it possible to set this column in the displayed table as editable, and it is smart enough to automatically generate a dropdown choice presenting all the possible books from the books table for the user to choose from.
However, this books table contains hundreds of choices, and I know that it will only make sense for the user to choose a book that is the same size as the pamphlet. So I would like to filter the options in the dropdown that Django generates and only show the books that share the same size with the Pamphlet.
How can I accomplish this?
I looked here: How do I filter ForeignKey choices in a Django ModelForm? but this doesn't seem to be referring to Django's Admin functionality.