我的模型有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')
答案 0 :(得分:14)
您要搜索的是:
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'))
答案 1 :(得分:4)