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

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

from django.db import models
class Category(models.Model):
name = models.CharField(max_length=100)
slug = models.SlugField(max_length=100, db_index=True, unique=True)
def __unicode__(self):
return self.name
class Director(models.Model):
name = models.CharField(max_length=100)
slug = models.SlugField(max_length=100, db_index=True, unique=True)
def __unicode__(self):
return self.name
class Project(models.Model):
title = models.CharField(max_length=100)
slug = models.SlugField(max_length=100, db_index=True, unique=True)
director = models.ForeignKey(Director, related_name='projects_via_director')
categories = models.ManyToManyField(Category,
related_name='projects_via_category')
def __unicode__(self):
return self.title


category_list = Category.objects.prefetch_related('projects_via_category')
director_list = Director.objects.prefetch_related('projects_via_director')
other_cats = category_list.filter(
projects_via_category__director__slug='steven-spielberg')
print category_list # [<Category: Film>, <Category: Commercial>]print other_cats # [<Category: Film>]print category_list[0] in other_cats # Falseprint category_list[0] in other_cats # 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

  • Carlos at Apr 10, 2013 at 2:43 pm
    maybe need read de docu
    https://docs.djangoproject.com/en/1.5/ref/models/querysets/#select-related

    Cheers

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

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

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

    from django.db import models
    class Category(models.Model):
    name = models.CharField(max_length=100)
    slug = models.SlugField(max_length=100, db_index=True, unique=True)
    def __unicode__(self):
    return self.name
    class Director(models.Model):
    name = models.CharField(max_length=100)
    slug = models.SlugField(max_length=100, db_index=True, unique=True)
    def __unicode__(self):
    return self.name
    class Project(models.Model):
    title = models.CharField(max_length=100)
    slug = models.SlugField(max_length=100, db_index=True, unique=True)
    director = models.ForeignKey(Director, related_name='projects_via_director')
    categories = models.ManyToManyField(Category,
    related_name='projects_via_category')
    def __unicode__(self):
    return self.title


    category_list = Category.objects.prefetch_related('projects_via_category')
    director_list = Director.objects.prefetch_related('projects_via_director')
    other_cats = category_list.filter(
    projects_via_category__director__slug='steven-spielberg')
    print category_list # [<Category: Film>, <Category: Commercial>]print other_cats # [<Category: Film>]print category_list[0] in other_cats # Falseprint category_list[0] in other_cats # 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.

    --
    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.
  • Tom Evans at Apr 10, 2013 at 2:53 pm

    On Wed, Apr 10, 2013 at 2:26 PM, bradley griffiths wrote:
    Cross posted from: stack overflow.

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



    print category_list # [<Category: Film>, <Category: Commercial>]
    print other_cats # [<Category: Film>]
    print category_list[0] in other_cats # False
    print category_list[0] in other_cats # 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?
    I doubt this behaviour is expected, raise a ticket with full details.

    Cheers

    Tom

    --
    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:29p
activeApr 10, '13 at 2:53p
posts3
users3
websitedjangoproject.com

People

Translate

site design / logo © 2022 Grokbase