# 修复 device 关联字段 null

This commit is contained in:
xianfx 2023-02-15 18:07:33 +08:00
parent 937c457afd
commit d1d9a0610d
2 changed files with 23 additions and 7 deletions

View File

@ -115,6 +115,7 @@ def get_device(device_id: str, source: str) -> Optional[dict]:
class WeatherLogSerializer(serializers.ModelSerializer): class WeatherLogSerializer(serializers.ModelSerializer):
power = serializers.SerializerMethodField() power = serializers.SerializerMethodField()
device_name = serializers.SerializerMethodField()
data_time = serializers.SerializerMethodField() data_time = serializers.SerializerMethodField()
create_time = serializers.SerializerMethodField() create_time = serializers.SerializerMethodField()
@ -143,11 +144,29 @@ class WeatherLogSerializer(serializers.ModelSerializer):
'create_time', 'create_time',
] ]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.device = None
def get_power(self, obj): def get_power(self, obj):
# power max 12.6V, min 8.5V # power max 12.6V, min 8.5V
power = int((float(obj.power) - 8.5) / 4.1 * 100) power = int((float(obj.power) - 8.5) / 4.1 * 100)
return str(power) return str(power)
def get_device_name(self, obj):
weather_device_id = obj.device_id
device = cache.get(weather_device_id)
if device:
self.device = device
return device.device_name
qs = DeviceInfo.objects.filter(weather_device_id=weather_device_id)
if qs.count() > 0:
device = qs[0]
self.device = device
cache.set(weather_device_id, device, 60 * 60 * 24)
return device.device_name
return None
def get_data_time(self, obj): def get_data_time(self, obj):
return obj.data_time.strftime('%Y-%m-%d %H:%M:%S') return obj.data_time.strftime('%Y-%m-%d %H:%M:%S')
@ -192,13 +211,12 @@ class WeatherLogWithInfoSerializer(WeatherLogSerializer):
'point_y', 'point_y',
] ]
def __init__(self, *args, **kwargs): # def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) # super().__init__(*args, **kwargs)
self.device = None # self.device = None
def get_longitude(self, obj): def get_longitude(self, obj):
device = self.device device = self.device
print(device)
if device: if device:
if device.longitude: if device.longitude:
return device.longitude return device.longitude
@ -206,7 +224,6 @@ class WeatherLogWithInfoSerializer(WeatherLogSerializer):
def get_latitude(self, obj): def get_latitude(self, obj):
device = self.device device = self.device
print(device)
if device: if device:
if device.latitude: if device.latitude:
return device.latitude return device.latitude
@ -214,7 +231,6 @@ class WeatherLogWithInfoSerializer(WeatherLogSerializer):
def get_location_id(self, obj): def get_location_id(self, obj):
device = self.device device = self.device
print(device)
if device: if device:
if device.location_id: if device.location_id:
return device.location_id return device.location_id

View File

@ -116,7 +116,7 @@ class DeviceTempLogListAPIView(ListAPIView, RoleMixin, DeviceListMixin):
class WeatherLogListAPIView(ListAPIView, RoleMixin, WeatherStationListMixin): class WeatherLogListAPIView(ListAPIView, RoleMixin, WeatherStationListMixin):
#serializer_class = WeatherLogSerializer # serializer_class = WeatherLogSerializer
permission_classes = [IsAuthenticated] permission_classes = [IsAuthenticated]
filter_backends = (DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter) filter_backends = (DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
pagination_class = WeatherlogHistoryPagination pagination_class = WeatherlogHistoryPagination