[Rest API] How can I retrieve measures according to a type, depending to 3 tables

Hello,

Previously, I need to retrieve value from the Measures table, but I also need to sensor information from sensors table.

I could do that with

 # Get all measures from the date range
        sensor_measures = Measures.objects \
            .filter(sensors_id_sensor=ids,
                    measure_created__range=[start,end]) \
            .order_by('measure_created') \
            .select_related('sensors_id_sensor')

and

class SensorTypeSerializer(ser.ModelSerializer):
    class Meta:
        fields = ("id_sensor_type","sensor_type_name","sensor_type_longname","measure_unit")
        model = SensorTypes


class TypesSerializer(ser.ModelSerializer):

    sensor_types_id_sensor_type=SensorTypeSerializer(
        read_only=True
    )

    #"sensor_type_name", "measure_unit",
    class Meta:
        fields = ("id_sensor","sensor_name","sensor_types_id_sensor_type")
        model = Sensors

Now, I need to get the value (measures table) according to serveral sensors (sensors table) which are from the same type (sensor_type table)
Models.py

Until now, I could get all sensors according to a type of sensor

class TypeViewSet(viewsets.ReadOnlyModelViewSet):
    """
    API: return the measures of several sensors according to a type of sensors and a date range.
    A type is generaly sensors with the same unit (°C, %, kPa)
    """
    # See serializers.py
    serializer_class = TypesSerializer

    def get_queryset(self):
        # get the params
        idt = self.kwargs['idtype']
        # get the date "from"
        start = self.request.query_params.get('start')
        # get the date "to"
        end = self.request.query_params.get('end')

        type = SensorTypes.objects.filter(id_sensor_type=idt)


        sensors = Sensors.objects.filter(sensor_types_id_sensor_type=idt)\
            .select_related('sensor_types_id_sensor_type')

        print(sensors.query)
        return sensors

and

class SensorTypeSerializer(ser.ModelSerializer):
    class Meta:
        fields = ("id_sensor_type","sensor_type_name","sensor_type_longname","measure_unit")
        model = SensorTypes


class TypesSerializer(ser.ModelSerializer):

    sensor_types_id_sensor_type=SensorTypeSerializer(
        read_only=True
    )

    #"sensor_type_name", "measure_unit",
    class Meta:
        fields = ("id_sensor","sensor_name","sensor_types_id_sensor_type")
        model = Sensors

here is the result

It’s nice but the values are missing :slight_smile:
I thought to start again as I did for my first exemple above, but the diffrence with this one, i retrieve the value according to ONE sensors

Now, I need the value according serveral sensors of the same type.
I was wondering how can I have three join as I did with select_related.

Or from this

sensor_measures = Measures.objects \
            .filter(sensors_id_sensor=ids,
                    measure_created__range=[start,end]) \
            .order_by('measure_created') \
            .select_related('sensors_id_sensor')

I am wondering if I can replace ids (sensors_id_sensor=ids,) with and dictionary of sensor id
The best would be to have all value for the related the id_sensor

Many thanks

Aïe, I just realized, the task is a bit more complex.

I also have to filter the sensors of a station according to a field
If you look at that picture

You can see, there is a relationship between the sensors and the stations table. The Stations table contains a field fields_id_field, and I need to filter the sensors that belong to a station which belong to a selected field.

For that reason I changed the url requets to

http://127.0.0.1:8080/api/map/field/1/type/2/?start=&end=2022-05-21%2010:10:10

my path look like

path("field/<int:idfield>/type/<int:idtype>/", TypeViewSet.as_view({'get': 'list'}), name="type_view_set"),

and I get the params values as the following

idt = self.kwargs['idtype']
idf = self.kwargs['idfield']

Now, my api need to retrieve the measures according to 4 tables

  • Measures contains all measures according to an ID of the sensors
  • Sensors contains the sensor information and the type of the sensor
  • sensor_types contains the information of the type (type name, unit, etc)
  • Stations contains the fields_id_field

I would really appreciate your help because I really have no idea how can create a queryset on 4 tables

  • Stations tables is needed to filter the queryset by field
  • sensor_types table is needed to filter queryset by type
  • Measures table is need to retrieve all measures by sensors (a sensor is attached to a station)

Many thanks for any help you can give me to archive my goal!!
Feel free to ask me more detail, if I missed some information which can help you to better understand my script.

Is there a reason why you can’t traverse the relationships between the tables? Such as to filter a measures queryset by the stations table you’d do:

Measures.objects.filter(
    sensors_id_sensor__stations_id_station__fields_id_field=some_field
)

I think the relevant documentation for this concept is here. If you need to filter on values from multiple areas, you can do so in one large .filter() call. You’ll want to test it out to determine if you need to use .distinct() or not though.

Dear Tim,
Thanks a lot. I have tried and I got a nice result. I will update the post when I will have compared the retrieved measures to make sure I have the right returned values

1 Like

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
    }
]

Each subsequent call to order_by is clearing the previous call. See the last line in the docs for order_by. Switch it to:

.order_by(
    '-sensors_id_sensor__stations_id_station',
    'sensors_id_sensor',
    'measure_created',
)

Thnaks, Tim, that’s pafect!!!
Have a good evening