如何在django查询中编写联接两个表的查询

如何在django查询中编写联接两个表的查询,django,django-queryset,Django,Django Queryset,表1: cam=models.CharField() temp=models.integer() 表2: cam=models.CharField() count=models.Integer() 任何人都可以使用django query编写上述查询。从您的描述来看,您的模型表1和表2应该是相关的,但是,您的代码中没有关系 因此,完全基于您的问题描述,解决方案应如下所示: table1_cam_list = [c.cam for c in Table1.objects.filter(temp=1

表1:

cam=models.CharField()

temp=models.integer()

表2:

cam=models.CharField()

count=models.Integer()


任何人都可以使用django query编写上述查询。

从您的描述来看,您的模型
表1
表2
应该是相关的,但是,您的代码中没有关系

因此,完全基于您的问题描述,解决方案应如下所示:

table1_cam_list = [c.cam for c in Table1.objects.filter(temp=10)]
table2_list = Table2.objects.filter(cam__in=table1_cam_list)
for item in table2_list:
    print(item.count) 
您可以使用从
Table1
获取
cam
列表,也可以使用该列表从
Table2
进行筛选。但如果您希望数据库中有一对多关系,则应使用字段

t1_cam_list=Table1.objects.filter(temp=10).values_list('cam', flat=True)
t2_list=Table2.objects.filter(cam__in=t1_cam_list).values_list('count',flat=True)

您的模型在哪里?模型名称是表1和表2。您的模型与字段之间是否存在某种关系?我们需要了解我是否已编辑了模型
table1_cam_list = [c.cam for c in Table1.objects.filter(temp=10)]
table2_list = Table2.objects.filter(cam__in=table1_cam_list)
for item in table2_list:
    print(item.count) 
t1_cam_list=Table1.objects.filter(temp=10).values_list('cam', flat=True)
t2_list=Table2.objects.filter(cam__in=t1_cam_list).values_list('count',flat=True)