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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With