how to create an empty queryset and to add objects manually in django

user2137817 picture user2137817 · Aug 15, 2013 · Viewed 38.9k times · Source

I need to create a queryset and to add manually some objects that i've got from different queries results in order to display it in a table. I uses xx=set() but it doesn't do the job.

Answer

karthikr picture karthikr · Aug 15, 2013

You can do it in one of the following ways:

from itertools import chain
#compute the list dynamically here:
my_obj_list = list(obj1, obj2, ...)
#and then 
none_qs = MyModel.objects.none()
qs = list(chain(none_qs, my_obj_list))

You could also do:

none_qs = MyModel.objects.none()
qs = none_qs | sub_qs_1 | sub_qs_2

However, This would not work for sliced querysets