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.
- 
        2can you show the relevant code ?karthikr– karthikr2013-08-15 14:52:03 +00:00Commented Aug 15, 2013 at 14:52
- 
        2How to create an empty queryset: docs.djangoproject.com/en/dev/ref/models/querysets/#noneBrandon Taylor– Brandon Taylor2013-08-15 14:54:55 +00:00Commented Aug 15, 2013 at 14:54
- 
        and how to add objects to it?user2137817– user21378172013-08-15 14:57:22 +00:00Commented Aug 15, 2013 at 14:57
- 
        You say "some objects" are they evaluated members of previous querysets or do you simply mean "various querysets" that you'd like to chain together?Ogre– Ogre2013-08-15 15:05:39 +00:00Commented Aug 15, 2013 at 15:05
- 
        1I don't understand why this question has so many upvotes - it is not even clear what the person is asking about.Burhan Khalid– Burhan Khalid2016-07-10 05:45:25 +00:00Commented Jul 10, 2016 at 5:45
                    
                        
                    
                 | 
            
                Show 3 more comments
            
        
         
    3 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
8 Comments
user2137817
 how to add dynamically?
  karthikr
 dynamically create a list, and then append it at once.
  karthikr
 actually, you dont need the 
  none qs. You could simply create a list and pass it in the context.dz210
 Does not work. type(qs) = list
  dz210
 @karthikr I am trying to manually create a queryset from a list of objects. My comment was a bit of a shorthand. The type of qs is still a list in the first code sample you gave - looking at it now it should be obvious.
   | 
 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.
5 Comments
user2137817
 the problem with the list that i can't display it on a table created from a model
  Daniel Roseman
 I have no idea what that means. Show some code.
  user2137817
 for x in xx:                  query=Xx.objects.filter(id=x.id)         lisst.append(query) table = getXtable(lisst)
  Daniel Roseman
 And am I supposed to guess what 
  getXtable is, or why it doesn't work?Gary
 Were you able to get this done? Even I want to pass a custom list instead of a querylist. I am unable to use this list method working. Its is giving me a valueerror from the admin view. I am using this in a 
  filter > queryset(self, request, queryset) The custom list is a created list and is not a querysetI'm really late to this one, but for a future reference you can create a queryset with all then filter for the objects that have a particular property.
Filtering on field object properties
Model.objects.filter(foo='bar')
Model.objects.exclude(foo='bar')
Filtering on non-field object properties
def return_queryset_with_desired_objects(self):
    qs = SomeModel.objects.all()
    wanted_ids = [obj.id for obj in qs if obj.foo]
    return self.filter(id__in=wanted_ids]
def return_queryset_without_undesired_objects(self):
    qs = SomeModel.objects.all()
    unwanted_ids = [obj.id for obj in qs if not obj.foo]
    return self.exclude(id__in=unwanted_ids]
4 Comments
mehmet
 unwanted_ids = [obj.id for obj in qs if not obj.foo] would mean a flock of trips to the database. Try unwanted_ids=SomeModel.objects.filter(foo=False).values_list('id', flat=True)lukeaus
 @mehmet I cleared up my answer. Hope it makes more sense now!
  mehmet
 I still think the list comprehension you do would bring the objects to the python side, probably in just 1 trip, but bringing entire objects would be more of a load on the database than just bringing a 
  values_list.lukeaus
 @mehmet You can't call a method on a values_list.
  

