On Tue, 2008-04-01 at 22:10 -0700, PENPEN wrote: > 1. About select_related(): > It is said that it will automatically "follow" foreign-key > relationships, selecting that additional related-object data when it > executes its query. So it is not applicable for ManyToMany > relationship, isn't it?
That's correct. It only follows non-nullable many-to-one relations. > And if select_related() is used, will the queryset method on the > related Manager access the DB later? For example: > b = Book.objects.select_related().get(id=4) > p = b.author # Doesn't hit the database. > p.get(pk=3) # Will this action hit the database? If 'author' is marked as a ForeignKey on the Book model, then b.author is an Author object. So b.author.get() makes no sense. > 2. About iterator(), 'for' and 'enumerate' on queryset: > There is a snippet from the django document page(http:// > www.djangoproject.com/documentation/db-api/): [... snip ...] > It seems that 'for' loop and enumerate on a QuerySet will use its > iterator. Then if I use 'for' loop or enumerate() on a queryset to do > some logical task and in the template it will iterate the QuerySet > again to display all the data, will the database be hit again? Looping over an iterable object does not call the "iterator()" method. It calls the __iter__() method. If you have a look at the definition of __iter__ in query.py, you'll notice that it doesn't call iterator() if the result_cache is populated. Regards, Malcolm -- I've got a mind like a... a... what's that thing called? http://www.pointy-stick.com/blog/ --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---