我有 3 个 Django 模型,其中第一个有第二个的外键,第二个有第三个的外键。像这样:
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)
例如,我想询问其作者在 2006 年出版书籍的所有文学代理人。我怎么能在 Django 中做到这一点?我查看了有关过滤器和查询集的文档,但没有看到明显的方法。谢谢。
最佳答案
LitAgent.objects.filter(author__book__year_published=2006)
关于python - 使用 Django 过滤三个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2422668/