Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 906
user2137817 Avatar asked Aug 15 '13 14:08

user2137817


People also ask

Which can be used to retrieve an object directly instead of a Queryset?

Retrieving Single Objects from QuerySets We can do this using the get() method. The get() returns the single object directly. Let's see the following example. As we can see in both examples, we get the single object not a queryset of a single object.


2 Answers

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

like image 153
karthikr Avatar answered Oct 01 '22 18:10

karthikr


You can't do that. A queryset is a representation of a database query. You can't add items to it manually.

But if you need an arbitrary ordered collection of model instances, just use a list.

like image 34
Daniel Roseman Avatar answered Oct 01 '22 19:10

Daniel Roseman