如何创建一个Django查询过滤器来比较同一模型中的两个日期字段 [英] How to create a Django queryset filter comparing two date fields in the same model

查看:101
本文介绍了如何创建一个Django查询过滤器来比较同一模型中的两个日期字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在我的Solr索引中获取活动记录过期的查询。
我想检查数据库中的 Activity.updated 日期是否大于 Activity.added_toSolr_date 为相同的记录。

Trying to get a query where the Activity record is stale in my Solr Index. I want to check to see if the Activity.updated date in the database is greater than the Activity.added_toSolr_date for the same record.

stale_activities_queryset = Activity.objects.filter(updated__gte = self.added_toSolr_date) 

模型

class Activity(models.Model):
    # Last time entry / metric was updated in the Activity model database
    updated =  models.DateTimeField( verbose_name="CRUD date")
    # When it was added to Solr Index Date
    added_toSolr_date = models.DateTimeField(blank=True, null=True, verbose_name="Added to Solr Index Date")

我引用了Django Query文档:
https: //docs.djangoproject.com/en/1.4/ref/models/querysets/
样本的单元测试:
https://github.com/django/django/blob/master/tests/modeltests/or_lookups/tests.py

I referenced Django Query docs: https://docs.djangoproject.com/en/1.4/ref/models/querysets/ And unit tests for samples: https://github.com/django/django/blob/master/tests/modeltests/or_lookups/tests.py

此处还在Stackoverflow中搜索。所有例子都使用输入的日期,而不是比较同一模型中的两个日期字段。

Also searched here on Stackoverflow. All the examples use an entered date instead of comparing two date fields in the same model.

推荐答案

F对象



F objects.

from django.db.models import F
stale_activities = Activity.objects.filter(updated__gte=F('added_toSolr_date')) 

这篇关于如何创建一个Django查询过滤器来比较同一模型中的两个日期字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆