38

It is possible to limiting QuerySet in this kind of way:

creators_list = ['jane', 'tarzan', 'chita']
my_model.objects.filter(creator=creators_list)

???

tshepang
  • 12,111
  • 21
  • 91
  • 136
krzyhub
  • 6,285
  • 11
  • 42
  • 68

2 Answers2

73

You mean like this?

my_model.objects.filter(creator__in=creator_list)

Docs: http://docs.djangoproject.com/en/dev/ref/models/querysets/#in

EDIT

This is now a bit outdated. If you run into problems with the original code, try this:

from django.db.models import Q

my_filter_qs = Q()
for creator in creator_list:
    my_filter_qs = my_filter_qs | Q(creator=creator)
my_model.objects.filter(my_filter_qs)

There's probably a better way to do it but I'm not able to test it at the moment.

ryanjdillon
  • 17,658
  • 9
  • 85
  • 110
Bryce Siedschlaw
  • 4,136
  • 1
  • 24
  • 36
  • Assuming of course that the creator field is a CharField of some sort – Bryce Siedschlaw May 10 '11 at 21:00
  • 2
    @Bryce: Isn't the blessing of the Universe enough ;)? – Uku Loskit May 10 '11 at 21:04
  • 1
    Do You assuming that there is something more than Universe? – krzyhub May 10 '11 at 21:07
  • Thanks for link to django's docs Bryce. I written this but in the past and my knowledge is better in one part of django, because of time flowing but I want to learn something from people like You in stright way:) – krzyhub May 10 '11 at 21:12
  • 2
    @Chris perhaps there is more then one Universe and perhaps we are just part of some multiverse :p, you never know. – Davor Lucic May 10 '11 at 21:13
  • Multiworlds? Could be, perhaps. But what do You think about idea that everything is just a working computer? Diuglas Adams written in one of his book: "What is the meaning of life, Universe, and everything? 42" :D – krzyhub May 10 '11 at 21:18
  • I can't get `__in` to work with chars even when the field is a `CharField`. And according to the docs it only works with integers. – dan-klasson Feb 02 '14 at 16:37
  • It might be outdated now as this was posted about 3 years ago but I'm not seeing anything in the docs where it says that it only works with integers. It even shows an example where it is using a subquery to dynamically find a list of strings and using those results as the lookup parameter. What does your query look like? – Bryce Siedschlaw Feb 02 '14 at 19:56
  • You may have to use a subquery like they show in the docs example. `inner_qs = Blog.objects.filter(name__contains='Ch').values('name')` `entries = Entry.objects.filter(blog__name__in=inner_qs)` – Bryce Siedschlaw Feb 02 '14 at 19:58
1

Also if you're using sqlite and running into problems, there exists a limitation for the max number of items in the list.

def divideChunks(l, n):
    for i in range(0, len(l), n):
        yield l[i:i + n]

for slicerange in divideChunks(objs, 10):
        myobjects = my_model.objects.filter(creator__in = slicerange)
        ...
  • just a suggestion, never iterate on db... they have indexing and other features... that's why they are there for... they are not just array... way way more than that... this will dramatically reduce the performance... please check the answer below: https://stackoverflow.com/a/5956422/10305444 – Maifee Ul Asad Oct 11 '21 at 05:44