我的模型有 3 个字段
class Table(models.Model):
in_time = models.DateTimeField(null=True, blank=True)
actual_time = models.DateTimeField(null=True, blank=True)
我想以这种方式获取结果:
select * from Table where in_time > '2013-12-31 00:00:00' and in_time != actual_time
谁能帮我完成这个
result = Table.objects.filter(in_time__gte = '2013-12-31 00:00:00')
最佳答案
您要搜索的是:
https://docs.djangoproject.com/en/dev/topics/db/queries/#filters-can-reference-fields-on-the-model
解决方案:
from django.db.models import F
from datetime import datetime
min_date = datetime(2013,12,31)
result = Table.objects.filter(in_time__gte=min_date).\
exclude(in_time__eq=F('actual_time'))
关于python - Django - 查询给出一列不等于同一模型中另一列的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21022626/