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
rahmc...@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+unsubscr...@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.


Reply via email to