tags:

views:

35

answers:

1

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.

+4  A: 
LitAgent.objects.filter(author__book__year_published=2006)
Ignacio Vazquez-Abrams
It works, thanks.
Vanessa MacDougal