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.
            Asked
            
        
        
            Active
            
        
            Viewed 4.8k times
        
    40
            
            
        - 
                    2can you show the relevant code ? – karthikr Aug 15 '13 at 14:52
- 
                    2How to create an empty queryset: https://docs.djangoproject.com/en/dev/ref/models/querysets/#none – Brandon Taylor Aug 15 '13 at 14:54
- 
                    and how to add objects to it? – user2137817 Aug 15 '13 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 Aug 15 '13 at 15:05
- 
                    various querysets results to display in a table – user2137817 Aug 15 '13 at 15:07
- 
                    This answer may help: [link](http://stackoverflow.com/a/432666/671485) – Ogre Aug 15 '13 at 15:14
- 
                    1I don't understand why this question has so many upvotes - it is not even clear what the person is asking about. – Burhan Khalid Jul 10 '16 at 05:45
- 
                    Were you able to get this done? Even I want to pass a custom list instead of a querylist. – Gary Feb 04 '19 at 14:36
3 Answers
36
            
            
        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
 
    
    
        karthikr
        
- 97,368
- 26
- 197
- 188
- 
                    
- 
                    3
- 
                    actually, you dont need the `none` qs. You could simply create a list and pass it in the context. – karthikr Aug 15 '13 at 15:25
- 
                    2
- 
                    @dz210 what are you trying to do? = is assignment, == is equality check. – karthikr Apr 20 '16 at 13:33
- 
                    2@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. – dz210 Apr 20 '16 at 15:18
- 
                    Were you able to get this done? Even I want to pass a custom list instead of a querylist. I am unable to use the chain method working. – Gary Feb 04 '19 at 14:36
- 
                    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 queryset – Gary Feb 04 '19 at 14:38
14
            
            
        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.
 
    
    
        Daniel Roseman
        
- 588,541
- 66
- 880
- 895
- 
                    the problem with the list that i can't display it on a table created from a model – user2137817 Aug 15 '13 at 15:23
- 
                    1
- 
                    for x in xx: query=Xx.objects.filter(id=x.id) lisst.append(query) table = getXtable(lisst) – user2137817 Aug 15 '13 at 15:27
- 
                    4And am I supposed to guess what `getXtable` is, or why it doesn't work? – Daniel Roseman Aug 15 '13 at 16:18
- 
                    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 queryset – Gary Feb 04 '19 at 14:38
14
            
            
        I'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]
 
    
    
        lukeaus
        
- 11,465
- 7
- 50
- 60
- 
                    1`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)` – mehmet Apr 28 '17 at 01:14
- 
                    
- 
                    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`. – mehmet Apr 29 '17 at 17:25
- 
                    
