Re: How do you extend a queryset object with another queryset as in list.extend(another_list)

1,126 views
Skip to first unread message

Javier Guerra Giraldez

unread,
Nov 6, 2012, 2:39:30 PM11/6/12
to django...@googlegroups.com


> filterList = accounts.objects.filter(name="foo", company="bar", blah blah blah..)
>
> itemsList = items.objects.filter(account__in=accountList, status="delivered", andSo="on"....)
>
>

Not sure if I understand it correctly, but shouldn't this work better?

items list=items.objects.filter(status="delivered",account__name="foo",account__company="bar",account__blah blah...)

If you want to separate where in the code you set the 'account' criteria from the 'items' ones, simply pass a dictionary with the arguments for the first .filter() and prep end 'account__' to all the keys before feeding to the items.objects.filter() call.

--
Javier

Kelly Nicholes

unread,
Nov 6, 2012, 2:53:29 PM11/6/12
to django...@googlegroups.com
You can always use itertools.chain() if you want to do it in memory.  Instead of doing it in memory as you've suggested, perhaps you need to index the account's PK so when your Items object manager queries for them, it can be quick.  

import itertools

list1 = [1,2,3,4]
list2 = ['a','b','c','d']

combined_list = itertools.chain(list1, list2)


On Tuesday, November 6, 2012 2:56:18 AM UTC-7, Neil Pritchard wrote:
Hi,

I have an application that needs to do a fairly big search across a large number of records that goes something like...

filterList = accounts.objects.filter(name="foo", company="bar", blah blah blah..)

itemsList = items.objects.filter(account__in=accountList, status="delivered", andSo="on"....)



The problem that I would like to overcome is that the 'account__in=accountList' creates some very inefficient SQL and slow's this query down a lot.

What I thought of doing instead was:

filterList = accounts.objects.filter(name="foo", company="bar", blah blah blah..)

for account in filterList:

    itemsList = items.objects.filter(account=account, status="delivered", andSo="on"....)

    # I know it's not possible to extend a django queryset, I'm looking for an equivalent
    wholeItemsList.extend(itemsList)


Now I know the .extend method won't work on a django queryset, and I realise that it's possible to convert the queryset to a list and than extend them but that would use a horrible ammount of memory to duplicate the queryset in this instance and wouldn't give me much of a time advantage.

What I would like to do is to add each of the querysets, searching for them without the '__in' filter saves a lot on sql, but then I don't want to loose the efficiency that would gain by adding another latency.

Any Ideas would be greatly appreciated....


Many thanks,

Neil


Marcello Bontempo Salgueiro

unread,
Nov 6, 2012, 3:41:26 PM11/6/12
to django...@googlegroups.com
Hi Neil, maybe its more easy accessing Foreign Key values [1].

Att,

Marcello.

[1] http://www.djangobook.com/en/2.0/chapter10.html

Enviado do meu Sony Ericsson Xperia mini

Neil Pritchard <ne...@neilpritchard.net> escreveu:


Hi,

I have an application that needs to do a fairly big search across a large number of records that goes something like...

filterList = accounts.objects.filter(name="foo", company="bar", blah blah blah..)

itemsList = items.objects.filter(account__in=accountList, status="delivered", andSo="on"....)



The problem that I would like to overcome is that the 'account__in=accountList' creates some very inefficient SQL and slow's this query down a lot.

What I thought of doing instead was:

filterList = accounts.objects.filter(name="foo", company="bar", blah blah blah..)

for account in filterList:

    itemsList = items.objects.filter(account=account, status="delivered", andSo="on"....)

    # I know it's not possible to extend a django queryset, I'm looking for an equivalent
    wholeItemsList.extend(itemsList)


Now I know the .extend method won't work on a django queryset, and I realise that it's possible to convert the queryset to a list and than extend them but that would use a horrible ammount of memory to duplicate the queryset in this instance and wouldn't give me much of a time advantage.

What I would like to do is to add each of the querysets, searching for them without the '__in' filter saves a lot on sql, but then I don't want to loose the efficiency that would gain by adding another latency.

Any Ideas would be greatly appreciated....


Many thanks,

Neil


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/32O_GEs1cUUJ.
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.
Reply all
Reply to author
Forward
0 new messages