I need to retrieve measures from my database.
In order to have a range of measures (I have measures from the last past 4 years), I get some with this queryset
Measures.objects.filter(measure_created__range=["2022-07-1 10:00:00", "2022-07-1 20:22:00"],sensors_id_sensor=me.id_sensor).order_by('-measure_created'):
In some case, the stations have been stopped, the latest measures is, for example, in june. Then if today I want to show the measure of the three last day, I have no measures.
In my previous code done with PHP, I query the latest measure in order to have the date/time, and then in my ‘from’ field I set the time 3 day prior of the latest date/time measure. You see?
I have no idea how I can adapt this code with Django
Measures.objects.filter(measure_created__range=["[date/time of latest measure] - [3 days]", "[date/time of latest measure]"],sensors_id_sensor=me.id_sensor).order_by('-measure_created'):
In order word, is there a way, to first query my database in order to have one row (I beleive something like limit?) in order to get the time, then calculate the time minus 3 days (or what ever), and setup the range with the two date?
Does that make sens?
latest= Measures.objects.filter(sensors_id_sensor=me.id_sensor).order_by('-measure_created')[:1] print("toto",latest) print("titi",latest.measure_created) Measures.objects.filter(measure_created__range=[latest.measure_created - timedelta(days=3), latest.measure_created],sensors_id_sensor=me.id_sensor).order_by('-measure_created')
Print toto return me, I gess 945045 rows!!!
toto <QuerySet [<Measures: Measures object (945045)>]>
and it does not like titi
I guess, I can fix the first problem, but I have no idea how to get the right date/time following my need.