gpt4 book ai didi

python - 名称 'Q' 未定义

转载 作者:太空宇宙 更新时间:2023-11-04 06:48:21 25 4
gpt4 key购买 nike

<分区>

您好,我正在使用 Django 编写一个搜索 View ,用于在预先填充的数据库中搜索校友。它将根据用户输入搜索预先填充的数据库以查找校友,并通过模板将其返回给用户。错误说名称“Q”未定义

def alumni_search(request, form_class=Find_AlumniForm, template_name='alumni/Find_Alumni.html'):
form = None
if request.method == 'POST':
#do search
form = form_class(request.POST)
if form.is_valid():
results = search(form.cleaned_data)
if results:
return render(request, template_name, {'form': form, 'Alumni': results})
else:
form = form_class()
return render(request, template_name, {'form': form})

def search(search_data):
q = Q()
results = None
searcher = alumni_search(search_data)

for key in search_data.iterkeys():
dispatch = getattr(searcher, 'search_%s' % key)
q = dispatch(q)
if q and len(q):
results = alumni.objects.filter(q).select_related()
#.order_by('-pk')
else:
results = []
return results

class AlumniSearch(object):
def __init__(self, search_data):
self.__dict__.update(search_data)

def search_keywords(self, q):
if self.keywords:
words = self.keywords.split()
first_name_q = Q()
last_name_q = Q()
for word in words:
first_name_q = first_name_q | Q(first_name__icontains=word)
last_name_q = last_name_q | Q(last_name__icontains=word)
keyword_q = first_name_q | last_name_q
q = q & keyword_q
return q

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com