2013-05-13 100 views
0

我的代碼中有下面的queryset。如何將django中的兩個queryset列表與第二個列表後的第一個列表結合起來

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) 

現在,我想創建一個新的列表(results_list),它將在開始處有new_list行,然後是most_viewed_list。

我嘗試過How to combine 2 or more querysets in a Django view?中提到的選項,但它們都沒有工作。

我試過下面選項...

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

但隨着)這個選項,如果我使用result_list.count(,它拋出一個錯誤。

And with results_list = new_list | most_viewd_list選項,我沒有得到所需的結果。

任何人都可以告訴我如何創建一個列表,其中new_list行後跟most_viewed_list行。

謝謝

回答

1

您的代碼正在創建一個列表(您想要的類型)。你用lenlen(result_list)作爲列表的長度。

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] 
相關問題