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.

8
  • 2
    can you show the relevant code ? Commented Aug 15, 2013 at 14:52
  • 2
    How to create an empty queryset: docs.djangoproject.com/en/dev/ref/models/querysets/#none Commented Aug 15, 2013 at 14:54
  • and how to add objects to it? Commented 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? Commented Aug 15, 2013 at 15:05
  • 1
    I don't understand why this question has so many upvotes - it is not even clear what the person is asking about. Commented Jul 10, 2016 at 5:45

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

8 Comments

how to add dynamically?
dynamically create a list, and then append it at once.
actually, you dont need the none qs. You could simply create a list and pass it in the context.
Does not work. type(qs) = list
@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.
|
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.

5 Comments

the problem with the list that i can't display it on a table created from a model
I have no idea what that means. Show some code.
for x in xx: query=Xx.objects.filter(id=x.id) lisst.append(query) table = getXtable(lisst)
And am I supposed to guess what getXtable is, or why it doesn't work?
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
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]

4 Comments

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 I cleared up my answer. Hope it makes more sense now!
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 You can't call a method on a values_list.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.