On Wed, Nov 2, 2011 at 6:26 PM, Ian Clelland <clell...@gmail.com> wrote: > Just looking at the source to QuerySet (finally), and it looks like the > __contains__ method actually does something different than this: It > evaluates the whole QuerySet in bulk at the database level, and starts > creating model instances based on that, but only until it finds a matching > one. So, after running "if obj in qs", you might end up with one object > created, or you might end up with 70M objects, or anywhere in between. > Again: odd, undocumented, and potentially surprising behaviour, and I'd > recommend explicit over implicit, especially in this case. >
I disagree. All of this behaviour is documented in my mind: It is documented here when querysets are evaluated: https://docs.djangoproject.com/en/1.3/ref/models/querysets/#when-querysets-are-evaluated It's documented here that iterating through a queryset will cache the objects created: https://docs.djangoproject.com/en/1.3/ref/models/querysets/#iterator It's documented here that testing for results is slightly quicker than evaluating and testing the queryset: https://docs.djangoproject.com/en/1.3/ref/models/querysets/#exists It's documented here that caching is optimistic - it caches the results that have been requested: https://docs.djangoproject.com/en/1.3/topics/db/queries/#caching-and-querysets Do we really need to specifically point out that the __contains__ method iterates through the queryset and stops when it finds an answer? Cheers Tom -- 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.