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

Community
  • 1
  • 1
Dev
  • 1,529
  • 4
  • 24
  • 36

2 Answers2

1

Your code is creating a list (of the type you want). You take the length of a list with len: len(result_list).

Marcin
  • 48,559
  • 18
  • 128
  • 201
1

Take an empty list like this

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

Ex:

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]
thegrinner
  • 11,546
  • 5
  • 41
  • 64
Ashish Jain
  • 760
  • 1
  • 8
  • 23