4

I'm building a social app which has a "like" feature, I'm using django-redis to store "like" numbers from every post like this:

from django_redis import get_redis_connection
con = get_redis_connection("default")
con.incr("post" + ":" + id)

From the doc Raw client access

It works great, and I also use django-rest-framework to provide api, ListCreateAPIView to list all posts.Usually it will display any fields you want in the database. The problem here is now I wanna display "like" count from django-redis in this API which means the json return are from my database and django-redis.

I had look around the source code about django-rest-framework

class ListCreateAPIView(mixins.ListModelMixin,mixins.CreateModelMixin,
                    GenericAPIView):
    """
    Concrete view for listing a queryset or creating a model instance.
    """
    def get(self, request, *args, **kwargs):
        return self.list(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        return self.create(request, *args, **kwargs)

and the list method:

class ListModelMixin(object):
    """
    List a queryset.
    """
    def list(self, request, *args, **kwargs):
        queryset = self.filter_queryset(self.get_queryset())

        page = self.paginate_queryset(queryset)
        if page is not None:
            serializer = self.get_serializer(page, many=True)
            return self.get_paginated_response(serializer.data)

        serializer = self.get_serializer(queryset, many=True)
        return Response(serializer.data)

It seems like I can only get data from the database queryset(which I use Mysql). Any possible way to return data from django-redis with django-rest-framework?

Solved: Thanks answer from @Rahul Gupta, I do a little hack to work better:

def get_likes(self, obj):
    post_id = obj.id
    post_like = get_redis_connection("default")
    likes = post_like.get("post"+":"+str(post_id))
    if likes == None:
        return 0
    else:
        likes = likes.decode('utf-8')
        return likes
4

2 回答 2

2

也许这不是您正在寻找的那种答案,但我的建议是看看Cacheops.

它为您的 django 模型提供了自动缓存/失效机制,因此您不必在代码中处理 redis 和 db 的不同查找,只需使用默认的 django orm,它会在后台为您处理内存缓存。如果您无论如何将喜欢的内容存储在 db 和 redis 中,我认为这是一个更好的解决方案。

于 2016-05-03T07:36:32.427 回答
2

您可以在您的中添加一个'likes' SerializerMethodFieldPostSerializer以添加likes对象的序列化表示。

class SocialPostSerializer(serializers.ModelSerializer):

    likes = serializers.SerializerMethodField() # define field

    class Meta:
        model = SocialPost

    def get_likes(self, obj):
        social_post_id = obj.id    
        # here write the logic to get the no. of likes 
        # for a social post using the 'social_post_id' from redis    
        return likes 

现在,DRF 在list请求中返回的序列化数据将包含一个参数likes以及其他参数。

于 2016-05-03T07:00:15.373 回答