Check dates in Django

0

I want to obtain data on the same date from this query:

Example:

fecha_inicio= 06/09/2017 fecha_final= 06/09/2017

    query=DetalleParte.objects.filter(fecha_creacion__range=(fecha_inicio,fecha_final)

However the Query only brings me data when start_date is greater or equal  and minor_date_date, but does not respect minor or equal

Likewise try to perform the same query but with lte and gte; However the  result is the same

query=DetalleParte.objects.filter(fecha_creacion__gte=fecha_inicio, fecha_creacion__lte=fecha_final)
    
asked by Noel L 06.09.2017 в 17:24
source

1 answer

1

This is because the range takes the dates from 12am in both cases, as indicated in the documentation:

  

Filtering a DateTimeField with dates will not include elements in the last   day, since the limits are interpreted as "0am on the given date". Yes   pub_date was a DateTimeField, the previous expression would become   this SQL: SELECT ... WHERE pub_date BETWEEN '2005-01-01 00:00:00' and   '2005-03-31 00:00:00';

You can see it at: link

I recommend that you modify the date by setting the time at 23:59, or better, adding one day to the 'final' date. With that you can solve the problem.

Ex:

from datetime import datetime, timedelta

fecha_inicio='06/09/2017'
fecha_final='06/09/2017'

d_fecha_inicio = datetime.strptime(fecha_inicio,'%d/%m/%Y')
d_fecha_final = datetime.strptime(fecha_final,'%d/%m/%Y') + timedelta(days=1)

query=DetalleParte.objects.filter(fecha_creacion__range=(d_fecha_inicio,d_fecha_final)

Greetings!

    
answered by 06.09.2017 в 17:32