Date__range inclusive in Django

1

I'm trying to do the following filter in a Django query:

ben_filtro = Auditoria.objects.filter(fecha__range=[fechaInicial,fechaFinal])

I get the results but excluding some that are in the final date. Fields are of type DateTimeField .

    
asked by jhon1946 23.02.2018 в 01:03
source

1 answer

1

Maybe what is happening to you is that the records of the last day of fechaFinal do not appear, this is due to the fact that if you use DateTimeField , it automatically interpresses the days, and the time sets the time leaves it as 00:00:00 In the documentation it explains here

To include the full day in fechaFinal , one solution may be to indicate a 23:59:59 to date, in some cases I used it as follows:

fechaFinalMax = datetime.datetime.combine(fechaFinal, datetime.time.max)

And then in the query I would use that variable fechaFinalMax

ben_filtro = Auditoria.objects.filter(fecha__range=[fechaInicial,fechaFinalMax])
    
answered by 23.02.2018 / 09:08
source