Comparing Object Fields with Django's ORM

﹥>﹥吖頭↗ 提交于 2019-12-02 07:54:25

S.Lott's answer is the way to go. Here's an example of using F:

class ModelA(models.Model):
    val = IntegerField()
    model_b = ForeignKey('ModelB')

class ModelB(models.Model):
    val = IntegerField()


>>> from django.db.models import F
>>> ModelA.objects.filter(val__lt=F('model_b__val'))
>>> print qs.query
SELECT `test_modela`.`id`, `test_modela`.`val`, `test_modela`.`model_b_id` FROM `test_modela` INNER JOIN `test_modelb` ON (`test_modela`.`model_b_id` = `test_modelb`.`id`) WHERE `test_modela`.`val` <  `test_modelb`.`val`
>>> 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!