FAQ
Corss posted from stack overflow<http://stackoverflow.com/questions/15923809/django-queryset-in-operator-fails-on-first-call>

When using the 'in' operator on a queryset, the first time the call is made
it fails.

from django.db import models

class Category(models.Model):
name = models.CharField(max_length=100)

class Project(models.Model):
categories = models.ManyToManyField(Category, related_name='projects')


category_list = Category.objects.prefetch_related('projects')

print category_list # [<Category: Category object>, <Category: Category object>]print category_list[0] in category_list # Falseprint category_list[0] in category_list # True

If I remove 'prefetch_related' and use 'all' instead then the problem is
gone, however this uses many more DB calls.

How can I get the correct result first time using prefetch_related?

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

Search Discussions

  • Peter of the Norse at Apr 13, 2013 at 2:52 pm

    On Apr 10, 2013, at 7:44 AM, bradley griffiths wrote:

    Corss posted from stack overflow

    When using the 'in' operator on a queryset, the first time the call is made it fails.

    from django.db import models


    class Category(models.Model):
    name = models.CharField(max_length=100)


    class Project(models.Model):
    categories = models.ManyToManyField(Category, related_name='projects')


    category_list = Category.objects.prefetch_related('projects')


    print category_list # [<Category: Category object>, <Category: Category object>]
    print category_list[0] in category_list # False
    print category_list[0] in category_list # True
    If I remove 'prefetch_related' and use 'all' instead then the problem is gone, however this uses many more DB calls.

    How can I get the correct result first time using prefetch_related?
    You might try converting the QuerySet into a list first. QuerySets avoid actually caching data for as long as possible.

    Peter of the Norse
    rahmcoff@radio1190.org



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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupdjango-users @
categoriesdjango, python
postedApr 10, '13 at 1:44p
activeApr 13, '13 at 2:52p
posts2
users2
websitedjangoproject.com

People

Translate

site design / logo © 2022 Grokbase