def get(self, request): host_obj = models.Host.objects.all() #对象只能是可迭代的,这个model对象会与传递给序列化类 hs = serializer.StatusSerializer(host_obj, many=True, context={'redis_obj': REDIS_OBJ }) #通过context传递额外参数给序列化类 data = hs.data return Response(data)
def hosts_status(request): hosts_data_serializer = serializer.StatusSerializer(request,REDIS_OBJ) hosts_data = hosts_data_serializer.by_hosts() return HttpResponse(json.dumps(hosts_data))
def hosts_status(request): # 获取序列化后的主机信息 hosts_data_serializer = serializer.StatusSerializer(REDIS_CONN) hosts_data = hosts_data_serializer.by_hosts() return HttpResponse(json.dumps(hosts_data))