首页 文章

Django - 跨关系查询

提问于
浏览
1

我是Django的新手,我正在尝试在Django中实现这种关系

人有汽车可以测试汽车问题汽车应该测试一组选定的标准

所以我实现它

class Person(model.Model):
    name = models.CharField(max_length=60)
    license = models.CharField(max_length=80)

class Car(models.Model):
    name = models.CharField()
    owner = models.ForeignKey('Person')
    isDiesel = models.BooleanField()

我试图将汽车领域导入测试 . 无论如何都有办法吗?我试图复制这个SQL语句

SELECT test FROM table WHERE OWNER IS x (object instance) AND CAR IS isDiesel

提前致谢 .

1 回答

  • 2
    Car.objects.filter(isDiesel=True, owner=person_instance)
    

    这将返回 Car 对象的数组 .

    你应该阅读at the Django docs on this subject;这个框架有一个非常糟糕的文档 .

相关问题