0

数据表中有2个字段都指向一个外键

class Student(models.Model):
  old_school = model.ForeignField(School)
  school = model.ForeignField(School)

我想找出所有old_school == school的行,请问这个怎么处理?
我试过了用:

Student.objects.all().exclude(old_school = school)
Student.objects.all().exclude(old_school__id = school__id)
Student.objects.all().exclude(old_school = self.school)

都不行。

sc2222 207
2014-01-16 提问
1 个回答
2

已采纳

我想找出所有old_school == school的行

from django.db.models import F
Student.objects.filter(old_school=F(“school”))

解释见官方doc

撰写答案

推广链接