我想收集所有应用程序的所有模型,然后过滤它们,但出现以下错误:
AttributeError at /search 'list' object has no attribute 'filter'
Request Method: GET Request
URL: http://127.0.0.1:8000/search?category=1&submit=%D9%BE%DB%8C%D8%AF%D8%A7+%DA%A9%D9%86%21
Django Version: 2.1.7 Exception Type: AttributeError Exception Value:
'list' object has no attribute 'filter' Exception
views.py
from django.shortcuts import render
from app_adventure.models import *
from app_cultural.models import *
from app_children.models import *
from app_base.choices import Tour_type_choices, Destination_choices, Day_choices, Month_choices
from itertools import chain
# Create your views here.
# search
def search(request):
adventure = AdventureTor.objects.all()
cultural = CulturalTor.objects.all()
children = ChildTor.objects.all()
queryset_list = list(chain(adventure, cultural, children))
# search by category
if 'category' in request.GET:
category = request.GET['category']
if category:
queryset_list = queryset_list.filter(category__icontains=category) # mitonest iexact bashe
context = {
'Tour_type_choices': Tour_type_choices,
'Destination_choices': Destination_choices,
'Day_choices': Day_choices,
'Month_choices': Month_choices,
'search': queryset_list,
}
return render(request, 'search/search.html', context)
答案 0 :(得分:1)
您无法过滤链,因为它不再是SQL查询。您可以尝试分别过滤这3个查询,然后使用chain()
。例如:
def search(request):
my_queries = [AdventureTor.objects.all(),
CulturalTor.objects.all(),
ChildTor.objects.all()]
# search by category
if 'category' in request.GET:
category = request.GET['category']
if category:
my_queries = [query.filter(category__icontains=category)
for query in my_queries]
queryset_list = list(chain(**my_queries))
context = {
...
答案 1 :(得分:0)
1
如错误所述,请在添加前过滤查询集