Django union查询引发数据库错误。(在复合语句的子查询中不允许ORDER BY。)

2024-05-15 23:33:54 发布

您现在位置:Python中文网/ 问答频道 /正文

当我尝试进行联合查询时,我的搜索栏不工作。 我的函数如下所示

def search(request):
query=request.GET['query']

allPostsTitle= Post.objects.filter(title__icontains=query)
allPostsAuthor= Post.objects.filter(author__icontains=query)
allPostsContent =Post.objects.filter(content__icontains=query)
allPosts=  allPostsTitle.union(allPostsContent, allPostsAuthor)
if allPosts.count()==0:
    messages.warning(request, "No search results found. Please refine your query.")
params={'allPosts': allPosts, 'query': query}
return render(request, 'home/search.html', params)

请帮我解决这个问题

我得到了这个错误

DatabaseError at /search
ORDER BY not allowed in subqueries of compound statements.
Request Method: GET
Request URL:    http://127.0.0.1:8000/search?query=continue
Django Version: 3.1.5
Exception Type: DatabaseError
Exception Value: ORDER BY not allowed in subqueries of compound statements.
Exception Location:virtualenvs/myvirtualenv/lib/python3.6/site- 
packages/django/db/models/sql/compiler.py, line 444, in get_combinator_sql
Python Executable:  /usr/local/bin/uwsgi
Python Version: 3.6.9

Tags: insearchgetobjectsrequestexceptionparamsfilter