Ah, ok that makes more sense now. Thank you!

On Aug 16, 5:52 pm, Alex Gaynor <alex.gay...@gmail.com> wrote:
> On Sun, Aug 16, 2009 at 8:42 AM, David<davidsh...@googlemail.com> wrote:
>
> > Hi all,
>
> > I'm a bit puzzled by the caching behaviour of QuerySets, when used in
> > iteration and when called with repr(). According to the documentation,
> > when a QuerySet is evaluated for the first time, the results are
> > cached.
>
> > Trying this out in the shell, this works as expected for iteration.
> > However when used with repr(), the QuerySet does not seem to be using
> > the cache, and I was just wondering if this is the correct behaviour.
>
> > I'll just illustrate this with a quick example. The Tag model has a
> > single field, name.
>
> > # starting with no tags
> >>>> Tag.objects.all()
> > []
>
> > # create a tag
> >>>> Tag.objects.create(name='one')
> > <Tag: one>
>
> > # as expected
> >>>> Tag.objects.all()
> > [<Tag: one>]
>
> > # assigning the QuerySet, as illustrated in documentation
> >>>> queryset = Tag.objects.all()
>
> > # evaluate, by implicitly calling repr()
> >>>> queryset
> > [<Tag: one>]
>
> >>>> Tag.objects.create(name='two')
> > <Tag: two>
>
> > # expecting queryset to use cache, which does not contain new tag
> >>>> queryset
> > [<Tag: one>, <Tag: two>]
>
> > # hmm, maybe not??
>
> > Any help on clarifying this would be greatly appreciated!
>
> > David
>
> The issue here is how __repr__ works on QuerySets, specifically it
> does repr(list(self[:MAX_REPR_SIZE])) that slicing causes it to create
> a new queryset, and so the result cache isn't populated in your
> QuerySet.
>
> Alex
>
> --
> "I disapprove of what you say, but I will defend to the death your
> right to say it." -- Voltaire
> "The people's good is the highest law." -- Cicero
> "Code can always be simpler than you think, but never as simple as you
> want" -- Me
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to