[Beginner] How can I know the time of latest measure

Hello!

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.

cheers

That’s because your query returns a queryset instead of an instance of Measures, as demonstrated by the output of your print(toto).

Instead of using the [:1] notation to return a list with only one element, you can use the first function to return the first instance.

You also have more “direct” access to the instance you are looking for by using the latest function.

And yes, your subsequent filter doesn’t work - again because of the incorrect object reference in the query.

Dear @KenWhitesell
Ouha, so many thanks again for your great help. I really appreciate.

I just tried the following

latest_measure = Measures.objects.filter(sensors_id_sensor=me.id_sensor).order_by('-measure_created').first()
print("Latest measure", latest_measure.measure_created)

for me in Measures.objects.filter(measure_created__range=[latest_measure.measure_created - timedelta(days=1), latest_measure.measure_created],sensors_id_sensor=me.id_sensor).order_by('-measure_created'):
    sondes['datas'].append({
        'measure_created': me.measure_created,
        'value': me.value,
        'sensors_id_sensor': me.sensors_id_sensor,
        'collections_id_collection': me.collections_id_collection,
    })

and I tried to manage the number of print by changing the value of the day(s)

timedelta(days=1)
timedelta(days=2)
timedelta(days=3)

and I could see a positif result.

I also prepare chartjs, and tomorrow I will work on the charts with the latest values of the 3 past days for each sensor

Many thanks for you help.
Have a good evening (or day)