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 <
[email protected]> 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 [email protected].
> To post to this group, send email to [email protected].
> 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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to