def get(self, request): # 获取用户信息 user = self.request.user user_id = user.id # 连接redis redis_conn = get_redis_connection('history') # 获取数据 history_sku_ids = redis_conn.lrange('history_%s' % user_id, 0, 5) skus = [] for sku_id in history_sku_ids: sku = SKU.objects.get(pk=sku_id) skus.append(sku) # 序列化 serializer = SKUSerializer(skus, many=True) return Response(serializer.data)
def get(self, request): user = request.user #1 从redis 获取数据 #1.1链接redis redis_conn = get_redis_connection('history') ids = redis_conn.lrange('history_%s' % user.id, 0, 4) #2. 根据id查询数据 skus = [] for id in ids: sku = SKU.objects.get(pk=id) skus.append(sku) #3.使用序列化器转换数据 serializer = SKUSerializer(skus, many=True) #4.返回响应 return Response(serializer.data)
def get(self, request): #获取用户user_id user_id = request.user.id #链接redis redis_conn = get_redis_connection('history') #获取数据 history_sku_ids = redis_conn.lrange('hsitory_%s' % user_id, 0, 5) skus = [] for sku_id in history_sku_ids: sku = SKU.objects.get(pk=sku_id) skus.append((sku)) #实现序列化器 serializers = SKUSerializer(skus, many=True) return Response(serializers.data)
def get(self, request): """获取浏览记录""" user_id = request.user.id # 查询redis list redis_conn = get_redis_connection("history") sku_id_list = redis_conn.lrange("history_%s" % user_id, 0, constants.USER_BROWSE_HISTORY_MAX_LIMIT) # 遍历获取数据 skus = [] for sku_id in sku_id_list: sku = SKU.objects.get(id=sku_id) skus.append(sku) # 序列化返回 serializer = SKUSerializer(skus, many=True) return Response(serializer.data)
def get(self, request): """ 获取 """ user_id = request.user.id redis_conn = get_redis_connection("history") history = redis_conn.lrange( "history_%s" % user_id, 0, constants.USER_BROWSING_HISTORY_COUNTS_LIMIT - 1) skus = [] # 为了保持查询出的顺序与用户的浏览历史保存顺序一致 for sku_id in history: sku = SKU.objects.get(id=sku_id) skus.append(sku) s = SKUSerializer(skus, many=True) return Response(s.data)