我想在每个孩子中搜索“查询”,并将结果呈现给list.html。我找不到解决方案,所以我决定在这里询问。这是我的代码:
def search_query_string(request, path=None, instance=None):
query = request.GET.get('query', False)
category = request.GET.get('category', False)
if category:
cat = get_object_or_404(Category, name=category)
children = cat.get_descendants(include_self=False)
ads = None
if query:
if category:
ads = Ad.objects.filter(Q(name__icontains=query) ) # I want to search query in children here
return render_to_response('ads/list.html', locals(), context_instance=RequestContext(request))
ads = Ad.objects.filter(Q(name__icontains=query))
return render_to_response('ads/list.html', locals(), context_instance=RequestContext(request))
如您所见,我已经有了子类别的列表,但仍然不知道如何搜索它们。请帮助:(
所以我终于弄清楚了该怎么做。在这里:
def search_query_string(request, path=None, instance=None):
query = request.GET.get('query', False)
category = request.GET.get('category', False)
query_list = []
if category:
parent = get_object_or_404(Category, name=category)
children = parent.get_descendants(include_self=False)
for c in children:
query_list.append(c.name)
print(query_list)
ads = None
if query:
if category:
ads = Ad.objects.filter(Q(name__icontains=query) & Q(category__name__in=query_list))
return render_to_response('ads/list.html', locals(), context_instance=RequestContext(request))
ads = Ad.objects.filter(Q(name__icontains=query))
return render_to_response('ads/list.html', locals(), context_instance=RequestContext(request))