无法获取查询集以返回按标签匹配降序的对象列表 [英] Cant get queryset to return list of objects that is descending by tag matches

查看:89
本文介绍了无法获取查询集以返回按标签匹配降序的对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行django 2.1.7,DRF并使用taggit。我正在编写自己的自定义查询集以查找对象具有的标签。
网址:
example.com/api/tags=books,headphones,sleep

I am running django 2.1.7, DRF and using taggit. I am writing my own custom queryset to find tags that an object has. The url: example.com/api/tags=books,headphones,sleep

应返回表示以下内容的JSON具有对象的顺序从包含最多标签到至少包含一个标签。
这是 gitgist

Should return JSON that has objects in order from contains most tags to contains at least one tag. Here is the gitgist

from django.db.models import Case, ExpressionWrapper, IntegerField, Q, Value, When

class SpecialSearch(ListAPIView):
    model = Object
    serializer_class = ObjectSerializer

    def get_queryset(self, rs, value):
        """
        Recipe search matching, best matching and kind of matching,
        by filtering against `tags` query parameter in the URL.
        """
        if value:
            tags = [tag.strip() for tag in value.split(',')]
            qs = Object.objects.filter(
                reduce(
                    lambda x, y: x | y, [Q(tags__icontains=tag) for tag in tags]))
            check_matches = map(
                lambda x: Case(
                    When(Q(tags__icontains=x), then=Value(1)),
                        default=Value(0)),
            tags)
            count_matches = reduce(lambda x, y: x + y, check_matches)
            qs = qs.annotate(
            matches=ExpressionWrapper(
                count_matches,
                output_field=IntegerField()))
            qs = qs.order_by('-matches')
        return qs

当前,此代码我提交了一些作品,但是在提交一系列新的标签时,返回的是由对象ID和API端点排序的json不会从API收到新的json转储。我现在完全迷路了。

Currently, this code I submitted kind of works but is returning json that is ordered by the object ID and the API endpoint when submitting a new series of tags won't receive a new json dump from the API. I am totally lost right now. Any help would be absolutely appreciated.

推荐答案

对于OP来说可能为时已晚,但万一有人来看看,请添加围绕count_matches的Sum()可能会解决问题:

Probably too late for the OP, but in case anyone comes by looking at this, adding Sum() around count_matches might do the trick:

from django.db.models import (Case, ExpressionWrapper, IntegerField, Q, Value, When, Sum)

class SpecialSearch(ListAPIView):
    model = Object
    serializer_class = ObjectSerializer

    def get_queryset(self, rs, value):
        """
        Recipe search matching, best matching and kind of matching,
        by filtering against `tags` query parameter in the URL.
        """
        if value:
            tags = [tag.strip() for tag in value.split(',')]
            qs = Object.objects.filter(
                reduce(
                    lambda x, y: x | y, [Q(tags__icontains=tag) for tag in tags]))
            check_matches = map(
                lambda x: Case(
                    When(Q(tags__icontains=x), then=Value(1)),
                        default=Value(0)),
            tags)
            count_matches = reduce(lambda x, y: x + y, check_matches)
            qs = qs.annotate(
            matches=ExpressionWrapper(
                Sum(count_matches),
                output_field=IntegerField()))
            qs = qs.order_by('-matches')
        return qs

这篇关于无法获取查询集以返回按标签匹配降序的对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆