Dear Tim,
Thanks a lot. I am happy with that
class TypeViewSet(viewsets.ReadOnlyModelViewSet):
"""
API: return the measures of several sensors according to a type of sensors for a selected field, and a date range.
(A type is generaly sensors with the same unit (°C, %, kPa))
If the time 'end' is empty, the today time will be considered
If the time 'start' is empty, the start time will take the value of the end time, minus 3 days.
Time is UTC zone (Universal Time Coordinated)
"""
# See serializers.py
serializer_class = MeasuresSerializer
def get_queryset(self):
# get the params
idt = self.kwargs['idtype']
idf = self.kwargs['idfield']
# get the date "from"
start = self.request.query_params.get('start')
# get the date "to"
end = self.request.query_params.get('end')
if end is None or len(end) <= 0:
end = datetime.datetime.now()
else:
end = datetime.strptime(end, '%Y-%m-%d %H:%M:%S')
if start is None or len(start) <= 0:
start = end - timedelta(days=3)
else:
start = datetime.strptime(start, '%Y-%m-%d %H:%M:%S')
print(start)
print(end)
m = Measures.objects.filter(
sensors_id_sensor__stations_id_station__fields_id_field=idf,
sensors_id_sensor__sensor_types_id_sensor_type=idt,
sensors_id_sensor__sensor_active=1,
measure_created__range=[start, end]
).order_by('-sensors_id_sensor__stations_id_station').order_by('sensors_id_sensor').order_by('measure_created')
#print(m.query)
return m
However, I have some difficultire to understand how to short. IF I only short by date, that’s fine. But if I want to
- first, order by id_station (small to hight)
- Then order by id_sensor (small to hight)
- and finally order by measures date (lastest to oldest)
It look, that it does not work as expected.
For example, with the above the stations id are not order. It look like only the time is considered
[
{
"id_measure": 813312,
"value": "4.3000",
"date": "2022-02-14T15:01:01Z",
"sensors_id_sensor": {
"id_sensor": 123,
"name": "b2",
"longname": "B3",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 35,
"longname": "Station B34",
"fields_id_field": 3
}
},
"collections_id_collection": 230870
},
{
"id_measure": 813313,
"value": "4.6000",
"date": "2022-02-14T15:01:01Z",
"sensors_id_sensor": {
"id_sensor": 124,
"name": "b3",
"longname": "BC2-3",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 35,
"longname": "Station B34",
"fields_id_field": 3
}
},
"collections_id_collection": 230870
},
{
"id_measure": 813310,
"value": "4.4000",
"date": "2022-02-14T15:01:01Z",
"sensors_id_sensor": {
"id_sensor": 109,
"name": "b1",
"longname": "B4",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 35,
"longname": "Station B34",
"fields_id_field": 3
}
},
"collections_id_collection": 230870
},
{
"id_measure": 813311,
"value": "4.2000",
"date": "2022-02-14T15:01:01Z",
"sensors_id_sensor": {
"id_sensor": 110,
"name": "b4",
"longname": "BC3-4",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 35,
"longname": "Station B34",
"fields_id_field": 3
}
},
"collections_id_collection": 230870
},
{
"id_measure": 813317,
"value": "4.3000",
"date": "2022-02-14T15:02:50Z",
"sensors_id_sensor": {
"id_sensor": 123,
"name": "b2",
"longname": "B3",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 35,
"longname": "Station B34",
"fields_id_field": 3
}
},
"collections_id_collection": 230871
},
{
"id_measure": 813318,
"value": "4.7000",
"date": "2022-02-14T15:02:50Z",
"sensors_id_sensor": {
"id_sensor": 124,
"name": "b3",
"longname": "BC2-3",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 35,
"longname": "Station B34",
"fields_id_field": 3
}
},
"collections_id_collection": 230871
},
{
"id_measure": 813315,
"value": "4.4000",
"date": "2022-02-14T15:02:50Z",
"sensors_id_sensor": {
"id_sensor": 109,
"name": "b1",
"longname": "B4",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 35,
"longname": "Station B34",
"fields_id_field": 3
}
},
"collections_id_collection": 230871
},
{
"id_measure": 813316,
"value": "4.2000",
"date": "2022-02-14T15:02:50Z",
"sensors_id_sensor": {
"id_sensor": 110,
"name": "b4",
"longname": "BC3-4",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 35,
"longname": "Station B34",
"fields_id_field": 3
}
},
"collections_id_collection": 230871
},
{
"id_measure": 813320,
"value": "11.7000",
"date": "2022-02-14T15:03:17Z",
"sensors_id_sensor": {
"id_sensor": 111,
"name": "b1",
"longname": "C5",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 40,
"longname": "Station C56",
"fields_id_field": 3
}
},
"collections_id_collection": 230872
},
{
"id_measure": 813325,
"value": "4.2000",
"date": "2022-02-14T15:04:38Z",
"sensors_id_sensor": {
"id_sensor": 123,
"name": "b2",
"longname": "B3",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 35,
"longname": "Station B34",
"fields_id_field": 3
}
},
"collections_id_collection": 230873
},
{
"id_measure": 813326,
"value": "4.6000",
"date": "2022-02-14T15:04:38Z",
"sensors_id_sensor": {
"id_sensor": 124,
"name": "b3",
"longname": "BC2-3",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 35,
"longname": "Station B34",
"fields_id_field": 3
}
},
"collections_id_collection": 230873
},
{
"id_measure": 813323,
"value": "4.3000",
"date": "2022-02-14T15:04:38Z",
"sensors_id_sensor": {
"id_sensor": 109,
"name": "b1",
"longname": "B4",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 35,
"longname": "Station B34",
"fields_id_field": 3
}
},
"collections_id_collection": 230873
},
{
"id_measure": 813324,
"value": "4.2000",
"date": "2022-02-14T15:04:38Z",
"sensors_id_sensor": {
"id_sensor": 110,
"name": "b4",
"longname": "BC3-4",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 35,
"longname": "Station B34",
"fields_id_field": 3
}
},
"collections_id_collection": 230873
},
{
"id_measure": 813330,
"value": "4.2000",
"date": "2022-02-14T15:06:21Z",
"sensors_id_sensor": {
"id_sensor": 123,
"name": "b2",
"longname": "B3",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 35,
"longname": "Station B34",
"fields_id_field": 3
}
},
"collections_id_collection": 230874
},
{
"id_measure": 813331,
"value": "4.5000",
"date": "2022-02-14T15:06:21Z",
"sensors_id_sensor": {
"id_sensor": 124,
"name": "b3",
"longname": "BC2-3",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 35,
"longname": "Station B34",
"fields_id_field": 3
}
},
"collections_id_collection": 230874
},
{
"id_measure": 813328,
"value": "4.3000",
"date": "2022-02-14T15:06:21Z",
"sensors_id_sensor": {
"id_sensor": 109,
"name": "b1",
"longname": "B4",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 35,
"longname": "Station B34",
"fields_id_field": 3
}
},
"collections_id_collection": 230874
},
{
"id_measure": 813329,
"value": "4.2000",
"date": "2022-02-14T15:06:21Z",
"sensors_id_sensor": {
"id_sensor": 110,
"name": "b4",
"longname": "BC3-4",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 35,
"longname": "Station B34",
"fields_id_field": 3
}
},
"collections_id_collection": 230874
},
{
"id_measure": 813333,
"value": "4.3000",
"date": "2022-02-14T15:08:10Z",
"sensors_id_sensor": {
"id_sensor": 109,
"name": "b1",
"longname": "B4",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 35,
"longname": "Station B34",
"fields_id_field": 3
}
},
"collections_id_collection": 230875
},
{
"id_measure": 813334,
"value": "4.2000",
"date": "2022-02-14T15:08:10Z",
"sensors_id_sensor": {
"id_sensor": 110,
"name": "b4",
"longname": "BC3-4",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 35,
"longname": "Station B34",
"fields_id_field": 3
}
},
"collections_id_collection": 230875
},
{
"id_measure": 813335,
"value": "4.2000",
"date": "2022-02-14T15:08:10Z",
"sensors_id_sensor": {
"id_sensor": 123,
"name": "b2",
"longname": "B3",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 35,
"longname": "Station B34",
"fields_id_field": 3
}
},
"collections_id_collection": 230875
},
{
"id_measure": 813336,
"value": "4.5000",
"date": "2022-02-14T15:08:10Z",
"sensors_id_sensor": {
"id_sensor": 124,
"name": "b3",
"longname": "BC2-3",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 35,
"longname": "Station B34",
"fields_id_field": 3
}
},
"collections_id_collection": 230875
},
{
"id_measure": 813339,
"value": "4.1000",
"date": "2022-02-14T15:11:21Z",
"sensors_id_sensor": {
"id_sensor": 116,
"name": "b4",
"longname": "C4",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 36,
"longname": "Station C34",
"fields_id_field": 3
}
},
"collections_id_collection": 230876
},
{
"id_measure": 813338,
"value": "4.3000",
"date": "2022-02-14T15:11:21Z",
"sensors_id_sensor": {
"id_sensor": 115,
"name": "b1",
"longname": "C3",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 36,
"longname": "Station C34",
"fields_id_field": 3
}
},
"collections_id_collection": 230876
},
{
"id_measure": 813343,
"value": "4.2000",
"date": "2022-02-14T15:11:42Z",
"sensors_id_sensor": {
"id_sensor": 123,
"name": "b2",
"longname": "B3",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 35,
"longname": "Station B34",
"fields_id_field": 3
}
},
"collections_id_collection": 230877
},
{
"id_measure": 813344,
"value": "4.4000",
"date": "2022-02-14T15:11:42Z",
"sensors_id_sensor": {
"id_sensor": 124,
"name": "b3",
"longname": "BC2-3",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 35,
"longname": "Station B34",
"fields_id_field": 3
}
},
"collections_id_collection": 230877
},
{
"id_measure": 813341,
"value": "4.3000",
"date": "2022-02-14T15:11:42Z",
"sensors_id_sensor": {
"id_sensor": 109,
"name": "b1",
"longname": "B4",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 35,
"longname": "Station B34",
"fields_id_field": 3
}
},
"collections_id_collection": 230877
},
{
"id_measure": 813342,
"value": "4.2000",
"date": "2022-02-14T15:11:42Z",
"sensors_id_sensor": {
"id_sensor": 110,
"name": "b4",
"longname": "BC3-4",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 35,
"longname": "Station B34",
"fields_id_field": 3
}
},
"collections_id_collection": 230877
},
{
"id_measure": 813349,
"value": "4.2000",
"date": "2022-02-14T15:13:31Z",
"sensors_id_sensor": {
"id_sensor": 123,
"name": "b2",
"longname": "B3",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 35,
"longname": "Station B34",
"fields_id_field": 3
}
},
"collections_id_collection": 230879
},
{
"id_measure": 813350,
"value": "4.5000",
"date": "2022-02-14T15:13:31Z",
"sensors_id_sensor": {
"id_sensor": 124,
"name": "b3",
"longname": "BC2-3",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 35,
"longname": "Station B34",
"fields_id_field": 3
}
},
"collections_id_collection": 230879
},
{
"id_measure": 813347,
"value": "4.3000",
"date": "2022-02-14T15:13:31Z",
"sensors_id_sensor": {
"id_sensor": 109,
"name": "b1",
"longname": "B4",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 35,
"longname": "Station B34",
"fields_id_field": 3
}
},
"collections_id_collection": 230879
},
{
"id_measure": 813348,
"value": "4.2000",
"date": "2022-02-14T15:13:31Z",
"sensors_id_sensor": {
"id_sensor": 110,
"name": "b4",
"longname": "BC3-4",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 35,
"longname": "Station B34",
"fields_id_field": 3
}
},
"collections_id_collection": 230879
},
{
"id_measure": 813354,
"value": "4.2000",
"date": "2022-02-14T15:15:14Z",
"sensors_id_sensor": {
"id_sensor": 123,
"name": "b2",
"longname": "B3",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 35,
"longname": "Station B34",
"fields_id_field": 3
}
},
"collections_id_collection": 230880
},
{
"id_measure": 813355,
"value": "4.5000",
"date": "2022-02-14T15:15:14Z",
"sensors_id_sensor": {
"id_sensor": 124,
"name": "b3",
"longname": "BC2-3",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 35,
"longname": "Station B34",
"fields_id_field": 3
}
},
"collections_id_collection": 230880
},
{
"id_measure": 813352,
"value": "4.3000",
"date": "2022-02-14T15:15:14Z",
"sensors_id_sensor": {
"id_sensor": 109,
"name": "b1",
"longname": "B4",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 35,
"longname": "Station B34",
"fields_id_field": 3
}
},
"collections_id_collection": 230880
},
{
"id_measure": 813353,
"value": "4.2000",
"date": "2022-02-14T15:15:14Z",
"sensors_id_sensor": {
"id_sensor": 110,
"name": "b4",
"longname": "BC3-4",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 35,
"longname": "Station B34",
"fields_id_field": 3
}
},
"collections_id_collection": 230880
},
{
"id_measure": 813363,
"value": "3.8000",
"date": "2022-02-14T15:19:46Z",
"sensors_id_sensor": {
"id_sensor": 69,
"name": "b1",
"longname": "B1",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 26,
"longname": "Station B12",
"fields_id_field": 3
}
},
"collections_id_collection": 230882
},
{
"id_measure": 813364,
"value": "4.2000",
"date": "2022-02-14T15:19:46Z",
"sensors_id_sensor": {
"id_sensor": 70,
"name": "b4",
"longname": "B2",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 26,
"longname": "Station B12",
"fields_id_field": 3
}
},
"collections_id_collection": 230882
},
{
"id_measure": 813367,
"value": "4.2000",
"date": "2022-02-14T15:20:51Z",
"sensors_id_sensor": {
"id_sensor": 92,
"name": "b4",
"longname": "C2",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 32,
"longname": "Station C12",
"fields_id_field": 3
}
},
"collections_id_collection": 230883
},
{
"id_measure": 813366,
"value": "4.4000",
"date": "2022-02-14T15:20:51Z",
"sensors_id_sensor": {
"id_sensor": 91,
"name": "b1",
"longname": "C1",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 32,
"longname": "Station C12",
"fields_id_field": 3
}
},
"collections_id_collection": 230883
},
{
"id_measure": 813369,
"value": "4.2000",
"date": "2022-02-14T15:21:42Z",
"sensors_id_sensor": {
"id_sensor": 67,
"name": "b1",
"longname": "A1",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 25,
"longname": "Station A12",
"fields_id_field": 3
}
},
"collections_id_collection": 230884
},
{
"id_measure": 813373,
"value": "4.1000",
"date": "2022-02-14T15:23:35Z",
"sensors_id_sensor": {
"id_sensor": 107,
"name": "b1",
"longname": "A3",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 34,
"longname": "Station A34",
"fields_id_field": 3
}
},
"collections_id_collection": 230885
},
{
"id_measure": 813374,
"value": "4.0000",
"date": "2022-02-14T15:23:35Z",
"sensors_id_sensor": {
"id_sensor": 108,
"name": "b4",
"longname": "A4",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 34,
"longname": "Station A34",
"fields_id_field": 3
}
},
"collections_id_collection": 230885
},
{
"id_measure": 813376,
"value": "4.5000",
"date": "2022-02-14T15:24:52Z",
"sensors_id_sensor": {
"id_sensor": 113,
"name": "b1",
"longname": "A5",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 38,
"longname": "Station A56",
"fields_id_field": 3
}
},
"collections_id_collection": 230886
},
{
"id_measure": 813377,
"value": "4.3000",
"date": "2022-02-14T15:24:52Z",
"sensors_id_sensor": {
"id_sensor": 114,
"name": "b4",
"longname": "B5",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 38,
"longname": "Station A56",
"fields_id_field": 3
}
},
"collections_id_collection": 230886
},
{
"id_measure": 813378,
"value": "4.2000",
"date": "2022-02-14T15:27:12Z",
"sensors_id_sensor": {
"id_sensor": 93,
"name": "b1",
"longname": "D1",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 33,
"longname": "Station D12",
"fields_id_field": 3
}
},
"collections_id_collection": 230887
},
{
"id_measure": 813379,
"value": "3.7000",
"date": "2022-02-14T15:27:12Z",
"sensors_id_sensor": {
"id_sensor": 94,
"name": "b4",
"longname": "D2",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 33,
"longname": "Station D12",
"fields_id_field": 3
}
},
"collections_id_collection": 230887
},
{
"id_measure": 813383,
"value": "3.7000",
"date": "2022-02-14T15:29:59Z",
"sensors_id_sensor": {
"id_sensor": 121,
"name": "b1",
"longname": "D3",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 37,
"longname": "Station D34",
"fields_id_field": 3
}
},
"collections_id_collection": 230889
},
{
"id_measure": 813384,
"value": "3.9000",
"date": "2022-02-14T15:29:59Z",
"sensors_id_sensor": {
"id_sensor": 122,
"name": "b4",
"longname": "D4",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 37,
"longname": "Station D34",
"fields_id_field": 3
}
},
"collections_id_collection": 230889
},
{
"id_measure": 813393,
"value": "4.1000",
"date": "2022-02-14T15:38:19Z",
"sensors_id_sensor": {
"id_sensor": 92,
"name": "b4",
"longname": "C2",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 32,
"longname": "Station C12",
"fields_id_field": 3
}
},
"collections_id_collection": 230891
},
{
"id_measure": 813392,
"value": "4.2000",
"date": "2022-02-14T15:38:19Z",
"sensors_id_sensor": {
"id_sensor": 91,
"name": "b1",
"longname": "C1",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 32,
"longname": "Station C12",
"fields_id_field": 3
}
},
"collections_id_collection": 230891
},
{
"id_measure": 813411,
"value": "4.4000",
"date": "2022-02-14T15:55:48Z",
"sensors_id_sensor": {
"id_sensor": 92,
"name": "b4",
"longname": "C2",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 32,
"longname": "Station C12",
"fields_id_field": 3
}
},
"collections_id_collection": 230894
},
{
"id_measure": 813410,
"value": "4.4000",
"date": "2022-02-14T15:55:48Z",
"sensors_id_sensor": {
"id_sensor": 91,
"name": "b1",
"longname": "C1",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 32,
"longname": "Station C12",
"fields_id_field": 3
}
},
"collections_id_collection": 230894
},
{
"id_measure": 813421,
"value": "4.6000",
"date": "2022-02-14T16:13:17Z",
"sensors_id_sensor": {
"id_sensor": 92,
"name": "b4",
"longname": "C2",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 32,
"longname": "Station C12",
"fields_id_field": 3
}
},
"collections_id_collection": 230896
},
{
"id_measure": 813420,
"value": "4.5000",
"date": "2022-02-14T16:13:17Z",
"sensors_id_sensor": {
"id_sensor": 91,
"name": "b1",
"longname": "C1",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 32,
"longname": "Station C12",
"fields_id_field": 3
}
},
"collections_id_collection": 230896
},
{
"id_measure": 813430,
"value": "4.4000",
"date": "2022-02-14T16:30:46Z",
"sensors_id_sensor": {
"id_sensor": 91,
"name": "b1",
"longname": "C1",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 32,
"longname": "Station C12",
"fields_id_field": 3
}
},
"collections_id_collection": 230898
},
{
"id_measure": 813431,
"value": "4.6000",
"date": "2022-02-14T16:30:46Z",
"sensors_id_sensor": {
"id_sensor": 92,
"name": "b4",
"longname": "C2",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 32,
"longname": "Station C12",
"fields_id_field": 3
}
},
"collections_id_collection": 230898
},
{
"id_measure": 813440,
"value": "4.3000",
"date": "2022-02-14T16:48:16Z",
"sensors_id_sensor": {
"id_sensor": 91,
"name": "b1",
"longname": "C1",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 32,
"longname": "Station C12",
"fields_id_field": 3
}
},
"collections_id_collection": 230900
},
{
"id_measure": 813441,
"value": "4.6000",
"date": "2022-02-14T16:48:16Z",
"sensors_id_sensor": {
"id_sensor": 92,
"name": "b4",
"longname": "C2",
"sensor_types_id_sensor_type": 2,
"stations_id_station": {
"id_station": 32,
"longname": "Station C12",
"fields_id_field": 3
}
},
"collections_id_collection": 230900
}
]