Filter across three tables using Django
Posted
by Vanessa MacDougal
on Stack Overflow
See other posts from Stack Overflow
or by Vanessa MacDougal
Published on 2010-03-11T05:07:14Z
Indexed on
2010/03/11
5:13 UTC
Read the original article
Hit count: 254
I have 3 django models, where the first has a foreign key to the second, and the second has a foreign key to the third. Like this:
class Book(models.Model):
year_published = models.IntField()
author = models.ForeignKey(Author)
class Author(models.Model):
author_id = models.AutoField(primary_key=True)
name = models.CharField(max_length=50)
agent = models.ForeignKey(LitAgent)
class LitAgent(models.Model):
agent_id = models.AutoField(primary_key=True)
name = models.CharField(max_length=50)
I want to ask for all the literary agents whose authors had books published in 2006, for example. How can I do this in Django? I have looked at the documentation about filters and QuerySets, and don't see an obvious way. Thanks.
© Stack Overflow or respective owner