Django REST to_representation:最后出现序列化器字段
Django REST to_representation: Have serializer fields appear last
我有以下序列化程序 class:
class DataLocationSerializer(QueryFieldsMixin, serializers.ModelSerializer):
def create(self, validated_data):
pass
def update(self, instance, validated_data):
pass
class Meta:
model = MeasurementsBasic
fields = ['temp', 'hum', 'pres', 'co', 'no2',
'o3', 'so2']
def to_representation(self, instance):
representation = super().to_representation(instance)
representation['timestamp'] = instance.time_received
return representation
数据在结构如下的 JSON 文件中返回:
{
"source": "ST",
"stations": [
{
"station": "ST1",
"data": [
{
"temp": -1.0,
"hum": -1.0,
"pres": -1.0,
"co": -1.0,
"no2": -1.0,
"o3": -1.0,
"so2": null,
"timestamp": "2021-07-04T21:00:03"
}
]
}
]
}
我怎样才能使时间戳出现在序列化程序的字段之前?
创建新词典:
def to_representation(self, instance):
representation = super().to_representation(instance)
return {'timestamp': instance.time_received, <strong>**representation</strong> }
我有以下序列化程序 class:
class DataLocationSerializer(QueryFieldsMixin, serializers.ModelSerializer):
def create(self, validated_data):
pass
def update(self, instance, validated_data):
pass
class Meta:
model = MeasurementsBasic
fields = ['temp', 'hum', 'pres', 'co', 'no2',
'o3', 'so2']
def to_representation(self, instance):
representation = super().to_representation(instance)
representation['timestamp'] = instance.time_received
return representation
数据在结构如下的 JSON 文件中返回:
{
"source": "ST",
"stations": [
{
"station": "ST1",
"data": [
{
"temp": -1.0,
"hum": -1.0,
"pres": -1.0,
"co": -1.0,
"no2": -1.0,
"o3": -1.0,
"so2": null,
"timestamp": "2021-07-04T21:00:03"
}
]
}
]
}
我怎样才能使时间戳出现在序列化程序的字段之前?
创建新词典:
def to_representation(self, instance):
representation = super().to_representation(instance)
return {'timestamp': instance.time_received, <strong>**representation</strong> }