获取日期大于今天或空日期的对象 [英] Get objects with date greater than today or empty date

查看:72
本文介绍了获取日期大于今天或空日期的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要选择日期字段大于今天date或date字段为空的所有模型对象。

I need to select all model objects with date field greater than today date OR date field empty.

我有以下代码:

@login_required
def event_new(request, person_uuid=None):
    today = datetime.datetime.today()
    #valid_until may be empty
    profile = Profile.objects.filter(company=request.user.company, valid_until__gte=today)

我需要选择所有valid_until字段为空或大于今天的Profile对象。我该如何实现?

I need to select all Profile objects with valid_until field empty or (if set) greater than today. How can I achieve this?

推荐答案

使用 Q

from django.db.models import Q

@login_required
def event_new(request, person_uuid=None):
    today = datetime.datetime.today()
    #valid_until may be empty
    profile = Profile.objects.filter(company=request.user.company).filter(Q(valid_until__gte=today)|Q(valid_until=None))

这篇关于获取日期大于今天或空日期的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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