Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get objects with date greater than today or empty date

Tags:

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?

like image 423
dease Avatar asked Jul 10 '13 10:07

dease


1 Answers

Use 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)) 
like image 137
falsetru Avatar answered Oct 24 '22 16:10

falsetru