manually add objects to a QuerySet

16,011 views
Skip to first unread message

chefsmart

unread,
Aug 8, 2010, 12:15:03 AM8/8/10
to Django users
I had asked this on stackoverflow, but I guess I couldn't explain
myself clearly enough. I'll try to ask again here:

Say I have two objects obj1 and obj2 of the same model (MyModel), now
I would like to add these objects to a new QuerySet. Can I create a
QuerySet manually like the following

my_qs = QuerySet(model=MyModel)

and then add obj1 and obj2 to this QuerySet like

my_qs.add(obj1)
my_qs.add(obj2)

Regards,
CM.


Masklinn

unread,
Aug 8, 2010, 4:25:07 AM8/8/10
to django...@googlegroups.com, Django users
What would the use case be, which would prevent you from using a normal list?
>

Lakshman Prasad

unread,
Aug 8, 2010, 4:33:22 AM8/8/10
to django...@googlegroups.com
You can't really add an object to queryset like that.

If you don't want to generate a list in the view, you can chain the querysets

chained_qs = chain(qs1,qs2)



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


chefsmart

unread,
Aug 8, 2010, 4:55:17 AM8/8/10
to Django users
The objects are coming from mutually exclusive querysets. I need to
pass a queryset of these objects to a function.

On Aug 8, 1:25 pm, Masklinn <maskl...@masklinn.net> wrote:

akaariai

unread,
Aug 8, 2010, 12:54:09 PM8/8/10
to Django users

On 8 elo, 11:55, chefsmart <moran.cors...@gmail.com> wrote:
> The objects are coming from mutually exclusive querysets. I need to
> pass a queryset of these objects to a function.

"Or" the querysets together?
In [2]: f1 = Foo1()
In [3]: f1.save()
In [4]: f2 = Foo1()
In [5]: f2.save()
In [6]: [f.pk for f in Foo1.objects.all()]
Out[6]: [1, 2]
In [7]: qs2 = Foo1.objects.filter(pk=1) | Foo1.objects.filter(pk=2) #
The oring of querysets
In [8]: [f.pk for f in qs2]
Out[8]: [1, 2]

- Anssi

chefsmart

unread,
Aug 8, 2010, 11:20:03 PM8/8/10
to Django users
Since I already have the objects, I don't want to hit the database
again. There are not just two but multiple objects, that's why I want
to avoid unnecessary db calls.

On Aug 8, 9:54 pm, akaariai <akaar...@gmail.com> wrote:
> On 8 elo, 11:55, chefsmart <moran.cors...@gmail.com> wrote:
>
> > The objects are coming from mutually exclusive querysets. I need to
> > pass aquerysetof these objects to a function.

chefsmart

unread,
Aug 9, 2010, 3:13:59 AM8/9/10
to Django users
I already have the objects in an earlier chunk of the code. I don't
want to hit the database again to get something I already have in
another form. That is what I mean when I say that code like my_qs =
MyModel.objects.filter(pk=obj1.pk) is utterly silly. I already have
the objects from earlier db queries; I just need them in a queryset.

Daniel Roseman

unread,
Aug 9, 2010, 4:22:45 AM8/9/10
to Django users
On Aug 9, 8:13 am, chefsmart <moran.cors...@gmail.com> wrote:
> I already have the objects in an earlier chunk of the code. I don't
> want to hit the database again to get something I already have in
> another form. That is what I mean when I say that code like my_qs =
> MyModel.objects.filter(pk=obj1.pk) is utterly silly. I already have
> the objects from earlier db queries; I just need them in a queryset.

But you haven't explained why converting the querysets to a list and
concatenating them, or just using itertools.chain(q1, q2), wouldn't
work. What sort of code do you have that relies on a queryset, and
doesn't work with anything else? Show it.
--
DR.

John M

unread,
Aug 11, 2010, 9:22:31 PM8/11/10
to Django users
I asked a similar question like, I want to sort a QS on a meta field,
can it be done? The answer worked, and is the same one you're
getting, use a list.

qs1 = model1.objects.filter(...)
qs2 = model2.objects.filter(...)

lqs1 = list(qs1)
lqs2 = list(qs2)

now you have something that is a python list that won't hit the db.

J
Reply all
Reply to author
Forward
0 new messages