简体   繁体   中英

Dynamic querys in Django using *args and **kwargs

I'm trying to create a search form where a user can choose from a few options to filter the search. I'm getting this error:

sortedByScore() argument after * must be a sequence, not Q

views.py:

def results(request):
    if request.method != "GET":
        return HttpResponse("request method needs to be GET")
    else:
        bound_search_form = SearchForm(request.GET)
        query = request.GET['query']
        subject = request.GET['subject']
        grade = request.GET['grade']
        order_by = request.GET['order_by']
        kwargs = {}
        args = ()
        if query:
            query_word_list = query.split()
            args = reduce(operator.or_, ((Q(title__contains=x) | Q(content__contains=x)) for x in query_word_list))
        if subject != 'all':
            kwargs["subject"] = subject

        summaries_list = Summary.objects.sortedByScore(*args, **kwargs)
        length = len(summaries_list)

        # pagination
        paginator = Paginator(summaries_list, 4)

        # get page number from GET request
        page_num = request.GET.get('page', 1)

        # get summaries from paginator according to page number
        try:
            summaries = paginator.page(page_num)
        except(EmptyPage, InvalidPage):
            summaries = paginator.page(paginator.num_pages)

        context_dict = {
            'sumAmount': length,
            'summaries': summaries,
            'search_form': bound_search_form,
        }

        return render(request, 'results.html', context_dict)

code for my custom manager sortedByScore:

class SummaryManager(models.Manager):
    def sortedByScore(self, *args, **kwargs):
        summaries = self.get_queryset().filter(*args, **kwargs)
        return sorted(summaries, key=lambda summary: summary.get_score(),reverse=True)
args = reduce(operator.or_, ((Q(title__contains=x) | Q(content__contains=x)) for x in query_word_list))

reduce reduces an iterable down to a single object, so after this line args is not an iterable. This reduces args down to a single Q object that OR s all of the items in query_word_list together. If you want to pass it using the * syntax, you'll need to wrap it in an iterable, ie args = (args,) .

Your first parameter to sortedByScore(*args, **kwargs) is a Q object:

args = reduce(operator.or_, ((Q(title__contains=x) | Q(content__contains=x)) for x in query_word_list))

You will need to redefine your method to pass in different parameters and/or perform the reduce inside the method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM