我有一个字段需要从get put隐藏,它需要放在Viewset的put中,我将如何实现?
我的序列化器如下所示,该字段将数据存储为JSON,所以我需要将其加载为JSON才能执行获取。但是在get上有原始字段(routing_data)会导致500错误,因此我需要将其隐藏在get中。但是当我使用看跌期权时,它将是我放入的该字段。
谢谢
serializers.py
class MonitoringSerializerRoutingTable(serializers.ModelSerializer):
hostname = serializers.ReadOnlyField(source='device.hostname', )
site_id = serializers.ReadOnlyField(source='device.site_id', )
rt = serializers.SerializerMethodField(source='routing_data',)
use = serializers.ReadOnlyField(source='device_use.use', )
def get_rt(self, instance):
try:
return json.loads(instance.routing_data)
except:
return instance.routing_data
class Meta:
model = DeviceData
fields = ('id','site_id','device_id','hostname','use', 'timestamp', 'rt','routing_data')
views.py
class MonitoringRoutingTableUpload(viewsets.ModelViewSet):
queryset = DeviceData.objects.select_related('device','device_use').order_by('monitoring_order')
serializer_class = MonitoringSerializerRoutingTable
permission_classes = (IsAdminUser,)
filter_class = DeviceData
filter_backends = (filters.SearchFilter,)
search_fields = ('device__hostname','device_use__use')
编辑 更新序列化程序似乎并没有更新数据
class MonitoringRoutingTableUpload(viewsets.ModelViewSet):
queryset = DeviceData.objects.select_related('device','device_use').order_by('monitoring_order')
permission_classes = (IsAdminUser,)
filter_class = DeviceData
filter_backends = (filters.SearchFilter,)
search_fields = ('device__hostname','device_use__use')
def get_serializer_class(self):
serializers = {
create: MonitoringCreateSerializer,
update: MonitoringCreateSerializer,
list: MonitoringSerializerRoutingTable,
retrieve: MonitoringSerializerRoutingTable,
}
return serializers.get(self.action)
serialiezr.py
class MonitoringSerializerRoutingTable(serializers.ModelSerializer):
hostname = serializers.ReadOnlyField(source='device.hostname', )
site_id = serializers.ReadOnlyField(source='device.site_id', )
rt = serializers.SerializerMethodField(source='routing_data',)
use = serializers.ReadOnlyField(source='device_use.use', )
def get_rt(self, instance):
try:
return json.loads(instance.routing_data)
except:
return instance.routing_data
class Meta:
model = DeviceData
fields = ('id','site_id','device_id','hostname','use', 'timestamp', 'rt')
class MonitoringCreateSerializer(serializers.ModelSerializer):
class Meta:
model = DeviceData
fields = ('id','site_id','device_id','routing_data')
PUT请求
URL: http://10.66.193.200:8100/api/rt_upload/9/
data: {"routing_data": "[{'subnet': '10.10.0.0/16', 'age': '6w3d', 'next_hop': '10.20.0.0/16'}, {'subnet': '10.30.0.0/16', 'age': '6w3d', 'next_hop': '10.40.0.0/16'}, {'subnet': '10.50.0.0/16', 'age': '6w3d'...}]"}
邮递员的回复:
{
"id": 9,
"site_id": 118,
"device_id": 460,
"hostname": "EDGE",
"use": "Remote Site Connectivity",
"timestamp": "2019-05-31T10:12:58.300252",
"rt": ""
}
答案 0 :(得分:1)
在您的序列化器字段中添加write_only = True
或
创建两个串行器,一个用于获取请求,另一个用于放置请求。 根据视图的get_serializer_class方法内的request方法更改序列化器。
答案 1 :(得分:0)
在这种情况下,可以对get和put / post使用不同的序列化器。使用所需的字段创建一个新的序列化器,然后删除serializer_class
属性并在视图中覆盖get_serializer_class
。像这样:
class MonitoringRoutingTableUpload(viewsets.ModelViewSet):
...
def get_serializer_class(self):
serializers = {
create: MonitoringCreateSerializer,
update: MonitoringCreateSerializer,
list: MonitoringSerializerRoutingTable,
retrieve: MonitoringSerializerRoutingTable,
}
return serializers.get(self.action)