0

I have below queryset in my code.

new_date = date.today() - timedelta(days=7)
most_viewd_list = mytable.objects.filter(show_on_website=True).order_by('-most_viewd')
new_list = most_viewd_list.filter(date_created__gte=new_date)

Now, i want to create a new list (results_list) which will have new_list rows at the beginning followed by most_viewed_list.

I have tried the options mentioned in How to combine 2 or more querysets in a Django view?, but none of them are working.

I tried below option...

 from itertools import chain
 result_list = list(chain(new_list, most_viewd_list))

But with this option if i use result_list.count(), it is throwing an error.

And with results_list = new_list | most_viewd_list option, i am not getting the desired result.

Can anyone tell me how i can create a list which will have new_list rows followed by most_viewed_list rows.

Thanks

4

2 回答 2

1

您的代码正在创建一个列表(您想要的类型)。len您使用:获取列表的长度len(result_list)

于 2013-05-13T17:32:34.167 回答
1

拿一个像这样的空列表

result_list=[]
result_list.append(most_viewd_list)
result_list.append(new_list)

前任:

a = [66.25, 333, 333, 1, 1234.5]
>>> print a.count(333), a.count(66.25), a.count('x')
2 1 0
>>> a.append(333)
>>> a
[66.25, 333, 333, 1, 1234.5, 333]
于 2013-05-13T17:58:59.053 回答