[Rest API] How can I create an alias in serializer file

Hello,

I can return a json format of my measures as the following

{
        "id_measure": 930570,
        "value": "12.0000",
        "date": "2022-06-08 01:26:55+00:00",
        "sensors_id_sensor": {
            "id_sensor": 72,
            "name": "te",
            "longname": "Température",
            "sensor_types_id_sensor_type": 2,
            "stations_id_station": {
                "id_station": 27,
                "longname": "Meteo Stevenson",
                "fields_id_field": 4
            }
        },
        "collections_id_collection": 262103
    },

As you can see I could renamed an index as the following

date = ser.CharField(source='measure_created')
name = ser.CharField(source='sensor_name')
longname = ser.CharField(source='sensor_longname')

more detail

class SensorsSerializer(ser.ModelSerializer):

    stations_id_station = StSerializer(
        read_only=True
    )
    name = ser.CharField(source='sensor_name')
    longname = ser.CharField(source='sensor_longname')

    class Meta:
        fields = ("id_sensor","name", "longname", "sensor_types_id_sensor_type", "stations_id_station")
        model = Sensors


class MeasuresSerializer(ser.ModelSerializer):

    sensors_id_sensor=SensorsSerializer(
        read_only=True
    )
    date = ser.CharField(source='measure_created')

    class Meta:
        fields = ("id_measure", "value", "date", "sensors_id_sensor", "collections_id_collection")
        model = Measures

My problem is with interger.

I would like to rename

  • sensors_id_sensor to sensor
  • stations_id_station to station
  • sensor_types_id_sensor_type to id_field
  • fields_id_field to id_field
{
        "id_measure": 930570,
        "value": "12.0000",
        "date": "2022-06-08 01:26:55+00:00",
        "sensor": {
            "id_sensor": 72,
            "name": "te",
            "longname": "Température",
            "id_type": 2,
            "station": {
                "id_station": 27,
                "longname": "Meteo Stevenson",
                "id_field": 4
            }
        },
        "collections_id_collection": 262103
    },

Then my first try

class MeasuresSerializer(ser.ModelSerializer):

    sensors_id_sensor=SensorsSerializer(
        read_only=True
    )
    date = ser.CharField(source='measure_created')
    sensor= ser.CharField(source='sensors_id_sensor')

    class Meta:
        fields = ("id_measure", "value", "date", "sensors_id_sensor", "sensor", "collections_id_collection")
        model = Measures
{
        "id_measure": 930570,
        "value": "12.0000",
        "date": "2022-06-08 01:26:55+00:00",
        "sensors_id_sensor": {
            "id_sensor": 72,
            "name": "te",
            "longname": "Température",
            "sensor_types_id_sensor_type": 2,
            "stations_id_station": {
                "id_station": 27,
                "longname": "Meteo Stevenson",
                "fields_id_field": 4
            }
        },
        "sensor": "Sensors object (72)",
        "collections_id_collection": 262103
    },

If I change to

sensor= ser.IntergerField(source='sensors_id_sensor')

message in the terminal
AttributeError: module 'rest_framework.serializers' has no attribute 'IntergerField'

If I remove sensors_id_sensor fom Fields, I got antoher error because of

sensors_id_sensor=SensorsSerializer(
        read_only=True
    )

Do you have some tips & hints to archive my goal?

edit:
here is an example of my models.py about sensors_id_sensor

class Measures(models.Model):
    id_measure = models.AutoField(primary_key=True)
    #sensors_id_sensor = models.ForeignKey('Sensors', models.DO_NOTHING, db_column='sensors_id_sensor')
    sensors_id_sensor = models.ForeignKey('Sensors', models.DO_NOTHING, related_name='sensor_n', db_column='sensors_id_sensor')
    collections_id_collection = models.ForeignKey(Collections, models.DO_NOTHING, db_column='collections_id_collection')
    value = models.DecimalField(max_digits=11, decimal_places=4)
    measure_created = models.DateTimeField()

    class Meta:
        managed = False
        db_table = 'measures'

The correct name would be ser.IntegerField

Ho, stupid I am. But it does not solve the problem.

When I try this

class MeasuresSerializer(ser.ModelSerializer):

    sensors_id_sensor=SensorsSerializer(
        read_only=True
    )
    date = ser.DateTimeField(source='measure_created')
    sensor = ser.IntegerField(source='sensors_id_sensor')

    class Meta:
        fields = ("id_measure", "value", "date", "sensor", "collections_id_collection")
        model = Measures

I got this error message, which is not surprising

The field ‘sensors_id_sensor’ was declared on serializer MeasuresSerializer, but has not been included in the ‘fields’ option.

if now I keep the sensors_id_sensor into Fields and I add sensor, beside

class MeasuresSerializer(ser.ModelSerializer):

    sensors_id_sensor=SensorsSerializer(
        read_only=True
    )
    date = ser.DateTimeField(source='measure_created')
    sensor = ser.IntegerField(source='sensors_id_sensor')

    class Meta:
        fields = ("id_measure", "value", "date", "sensors_id_sensor", "sensor", "collections_id_collection")
        model = Measures

the following message show up

int() argument must be a string, a bytes-like object or a number, not ‘Sensors’

If then I change sensor = ser.IntegerField(source='sensors_id_sensor')
to sensor = ser.CharField(source='sensors_id_sensor')

I have no error message, but the result is not was I am expected to have

{
        "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
            }
        },
        "sensor": "Sensors object (123)",
        "collections_id_collection": 230870
    },

What if you change here:

To:
sensor = ser.IntegerField(source='sensors_id_sensor_id')

Hello

class MeasuresSerializer(ser.ModelSerializer):

    sensors_id_sensor=SensorsSerializer(
        read_only=True
    )
    date = ser.DateTimeField(source='measure_created')
    sensor = ser.IntegerField(source='sensors_id_sensor')

    class Meta:
        fields = ("id_measure", "value", "date", "sensor", "collections_id_collection")
        model = Measures

Sadly, it does work because

sensors_id_sensor=SensorsSerializer(
        read_only=True
    )

has no reference in Fields yet. I also tried to change to

sensor=SensorsSerializer(
        read_only=True
    )

but I got a new error message

int() argument must be a string, a bytes-like object or a number, not ‘Sensors’

By chance, I also tried this sensor = ser.CharField(source='sensors_id_sensor') but the result, is not what I am expected for

{
        "id_measure": 813313,
        "value": "4.6000",
        "date": "2022-02-14T15:01:01Z",
        "sensor": "Sensors object (124)",
        "collections_id_collection": 230870
    },