6

After a search of a database I end up with an array of querysets. I wanted to concatenate these queryset somewhat like we can do with list elements. Is this possible or maybe there an altogether better way to do this? The end goal here is to get queryset for rows of table that contain one of a set of strings in a field.

for i in range(0,(searchDiff+1)):
    filterString = str(int(searchRange[0]) + i)
    myQuerySetTwoD.append(my.objects.filter(asn=filterString))
    for j in range(0,(len(myQuerySetTwoD)-1)):
        myQuerySet = myQuerySet + myQuerySetTwoD[j]

UPDATE: Found my own answer (something about writing the question down maybe)

Just

from itertools import chain

then replace

myQuerySet = myQuerySet + myQuerySetTwoD[j]

with

BgpAsnList = chain(BgpAsnList,BgpAsnListTwoD[j])
evolution
  • 4,332
  • 5
  • 29
  • 34

2 Answers2

5

I think the proper way to do that is to use the | operator (that is, if the QuerySets are of the same type):

qset = MyModel.objects.none()
for filterString in list_of_filterStrings:
    qset_temp = MyModel.objects.filter(asn=filterString)
    qset = qset | qset_temp
mccc
  • 2,354
  • 1
  • 20
  • 22
0

Your code looks weird! I have no idea how does it work and what you are doing here, but here is how I would do the query thing instead:

from django.db.models import Q

myQuery = Q()
for i in range(0,(searchDiff+1)):
    filterString = str(int(searchRange[0]) + i)
    myQueryTwoD.append(Q(asn=filterString))
    for j in range(0,(len(myQueryTwoD)-1)):
        myQuery = myQuery | myQueryTwoD[j]

myQuerySet = my.objects.filter(myQuery)

How does it work?

Blagh.objects.filter(Q(smth='A') | Q(smth='B'))
  will generate query which looks like:
SELECT ... from blagh WHERE smth = 'A' OR smth = 'B'

Take a look and django docs: Complex lookups with Q object

Your method with itertools will result in many queries to database. With my solution it will be one query with OR lookup in WHERE clause.

Update:

Maybe even better solution would be something like this:

strings = []
for i in range(0,(searchDiff+1)):
    filterString = str(int(searchRange[0]) + i)
    strings.append(filterString)

my_query_set = MyModel.objects.filter(arn__in=strings)

I fail to understand why do you need that inner loops...

Ski
  • 14,197
  • 3
  • 54
  • 64
  • It just about works alright when i stuck in 'chain' but you completely correct about the efficiency. It is best to to make the query more intelligent rather than running a loop of multiple queries. Thanks that helped a lot. – evolution Dec 13 '11 at 13:18
  • Ignore the code I came up with. What you have above is perfect for what I need. I just tried it there. One query. The '__in' is really tidy. I assume I could have ORed multiple Q() too but I have having difficulty getting this to work too. '__in' worked first time. – evolution Dec 13 '11 at 14:07