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'