在django中编写内连接查询

时间:2018-04-27 08:11:29

标签: mysql django django-models

我有两个表,需要在django

中编写此查询

我的模特是:

class students(models.Model):
    name = models.CharField(max_length=30)
    active = models.BooleanField(max_length=1)

class college(models.Model):
    department = models.CharField(max_length=20)
    college_name = models.CharField(max_length = 50)
    s_id = models.ForeignKey(students, on_delete=models.CASCADE)

我的mysql查询:

select b.name,a.department from college as a 
inner join students as b on (a.s_id = b.id) 
where (a.department = 'CSE' AND b.active = 1)

我只想在django中写这个查询

1 个答案:

答案 0 :(得分:1)

college.objects.filter(department='CSE',s_id__active=True).values('s_id__name','department')

使用此