I need to select all model objects with date field greater than today date OR date field empty.
我需要选择日期字段大于今天日期或日期字段为空的所有模型对象。
I have following code:
我有以下代码:
@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)
I need to select all Profile objects with valid_until field empty or (if set) greater than today. How can I achieve this?
我需要选择所有Profile对象,其中valid_until字段为空或(如果设置)大于今天。我怎样才能做到这一点?
1 个解决方案
#1
14
Use Q.
使用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))
#1
14
Use Q.
使用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))