Reputation: 341
def g_view(request):
header_category = Category.objects.all()
m = Type1.objects.all()
r=Type2.objects.all()
g=Type3.objects.all()
from itertools import chain
orders=list(sorted(chain(m,r,g),key=lambda objects:objects.start))
mquery = request.GET.get('m')
if mquery:
orders = orders.filter(
Q(name__icontains=mquery) |
Q(game__name__icontains=mquery) |
Q(teams__name__icontains=mquery)).distinct()
(Type is abstract and type1 type2 type3 are inherited classes)
I got this error 'list' object has no attribute 'filter'
Upvotes: 2
Views: 16019
Reputation: 51988
Basically when you chain
multiple queryset, you loose the ability of queryset. After chaining, they become part of an iterator. And you can access the values of iterator by iterating it or calling list
explicitly. You need to do the query before chaining the queryset.
query = Q(name__icontains=mquery) |
Q(game__name__icontains=mquery) |
Q(teams__name__icontains=mquery)
m = Type1.objects.filter(query).distinct()
r = Type2.objects.filter(query).distinct()
g = Type3.objects.filter(query).distinct()
orders=list(sorted(chain(m,r,g),key=lambda objects:objects.start))
Upvotes: 4