> There is a threaded comments app http://code.google.com/p/django- 
> threadedcomments/
> , Now that I think about it, they surely would be doing this
> conversion from relational to hierarchical format. I will read the
> code and let you know if I can find something useful.

While I had been searching on djangosnippets.org, I had completely  
overlooked searching (properly) on Google; I completely missed this app.
Thanks for the pointer!


>>    Hi folks,
>>
>> I'm somewhat new to Django, happily plodding along creating apps for
>> a website, when I stumbled upon oddities when slicing QuerySets. I'm
>> using 0.97pre (checked out from SVN about a month ago) on Mac OS X
>> 10.4, Python 2.5.
>> With the following very simply class:
>>
>> class Article(models.Model):
>>      text = models.CharField(max_length=100, default='lorem ipsum')
>>      def __unicode__(self):
>>          return self.text
>>
>> I can do the following:
>>>>> from dtest.models import Article
>>>>> for x  in range(1, 5):
>> ...     a = Article(text='article no %d' % x)
>> ...     a.save()
>> ...
>>>>> articles = Article.objects.all()
>>>>> for a in articles:
>> ...     print a
>> ...
>> article no 1
>> article no 2
>> article no 3
>> article no 4
>>
>> That all works fine. Things get weird when I slice the QuerySet (I'm
>> using the id-ing and type-ing to check on the object):
>>>>> selection = Article.objects.all()[0:3]
>>>>> print id(selection), type(selection)
>> 20614928 <class 'django.db.models.query._QuerySet'>
>>>>> for a in selection:
>> ...     print a
>> ...
>> article no 1
>> article no 2
>> article no 3
>>
>>>>> selection = Article.objects.all()[0:3]
>>>>> print id(selection), type(selection)
>> 20627632 <class 'django.db.models.query._QuerySet'>
>>>>> for i in range(4):
>> ...     print selection[i]
>> ...
>> article no 1
>> article no 2
>> article no 3
>> article no 4
>>
>> ???
>> This is what I didn't expect: where's the IndexError (or
>> ObjectNotFound)?
>>
>> But if I evaluate the selection before (using len(), to minimize
>> output; print also works, for example), it does work as I expect:
>>>>> selection = Article.objects.all()[0:3]
>>>>> print id(selection), type(selection)
>> 20615056 <class 'django.db.models.query._QuerySet'>
>>>>> len(selection)
>> 3
>>>>> print id(selection), type(selection)
>> 20615056 <class 'django.db.models.query._QuerySet'>
>>>>> for i in range(4):
>> ...     print selection[i]
>> ...
>> article no 1
>> article no 2
>> article no 3
>> Traceback (most recent call last):
>>    File "<console>", line 2, in <module>
>>    File "/sw/lib/python2.5/site-packages/django/db/models/query.py",
>> line 161, in __getitem__
>>      return self._result_cache[k]
>> IndexError: list index out of range
>>
>> It obviously has to do with how and when QuerySets are evaluated; it
>> looks like that before evaluation, the slice hasn't 'worked' yet. The
>> id() and type() statements suggest it's still completely the same
>> object though, before and after; nothing has apparently changed.
>> (In fact, this is the reason I'm not recycling the first articles or
>> selection QuerySet, but instead do an objects.all() each time; it
>> gets evaluated in the first for-loop, and when recycled, things work
>> as I expect).
>> I found a few threads on the mailing list on this, bit I'm not sure
>> if they explain this behaviour to me (threads referenced below).
>> Could someone point me in the right direction/documentation on this,
>> or just change how my mind works (nothing too drastically, please).
>> Do I really need to evaluate the selection beforehand?
>> Obviously, the example above is simplistic and should be done
>> differently, but I do have a situation where I'd like to be able to
>> index a sliced QueyrSet step by step, instead of iterating through
>> it. Or, what possibly could also solve my problem: can I iterate
>> through a (sliced) QuerySet step by step, by eg calling something
>> like a next() method each time I needed the next element? (I've
>> tried, but there's no next() method on QuerySets apparently).
>>
>> Thanks a lot,
>>
>>    Evert
>>
>> http://groups.google.com/group/django-users/browse_thread/thread/
>> aef03c62f655fe45/a6ce6074ceab7438?lnk=gst&q=queryset
>> +slicing#a6ce6074ceab7438http://groups.google.com/group/django- 
>> users/browse_thread/thread/
>> 3b98ffd38969c49a/a56354e0c0e61a9a?lnk=gst&q=queryset
>> +slicing#a56354e0c0e61a9a
> >


--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to