40

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.

user2137817
  • 1,825
  • 5
  • 28
  • 45

3 Answers3

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
  • how to add dynamically? – user2137817 Aug 15 '13 at 14:59
  • 3
    dynamically create a list, and then append it at once. – karthikr Aug 15 '13 at 15:00
  • 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
    Does not work. type(qs) = list – dz210 Apr 20 '16 at 13:10
  • @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
    I have no idea what that means. Show some code. – Daniel Roseman Aug 15 '13 at 15:23
  • for x in xx: query=Xx.objects.filter(id=x.id) lisst.append(query) table = getXtable(lisst) – user2137817 Aug 15 '13 at 15:27
  • 4
    And 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
  • @mehmet I cleared up my answer. Hope it makes more sense now! – lukeaus Apr 28 '17 at 22:19
  • 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
  • @mehmet You can't call a method on a values_list. – lukeaus Apr 30 '17 at 21:31