Hi all, The current descriptor implementation is inefficient for related object lookups. For example, using my 'Camp' model, which has a many-to-many 'leaders' relationship:
>>> c = Camp.objects.all()[1] >>> len(connection.queries) 1 >>> c.leaders.all() [Jason & Ruth Phillips] >>> len(connection.queries) 2 >>> c.leaders.all() [Jason & Ruth Phillips] >>> len(connection.queries) 3 >>> This isn't great. But I've got a partially working solution. The first step is to get rid of .all() for these lookups (hooray!), by making the related object descriptor return a QuerySet subclass instance instead of a Manager subclass instance. The second step is to make the descriptor objects cache the QuerySets once they have been created. Everything works after the first step, but there are no performance gains, only syntax benefits (and internally some better factored and simplified code). However, after the second step, 19 of the tests fail. The problems are with things like this in the test scripts: many_to_many/models.py: ----------------------- # If we delete an Article, its Publications won't be able to access it. >>> a2.delete() >>> Article.objects.all() [Django lets you build Web apps easily] >>> p1.article_set [Django lets you build Web apps easily] ----------------------- With my changes, the final line fails because the cache in p1 doesn't know that all the objects have been deleted, and p1.article_set still has a2 in it. It's not just deletion of course - Django's ORM doesn't guarantee that any changes done in one place will be updated to others. These test failures are really just instances of the following problem: http://groups.google.com/group/django-users/browse_thread/thread/56698424ae3708ea/69c023c56e4c7b64 In a web environment, these problems are really not a very big deal as Adrian explained in that thread. So, what do people think of these changes? We could add in some kind of explicit 'freshen' calls into the tests to make them pass. At the end of the day it's a performance/elegance tradeoff. Regards, Luke -- "You'll be glad to know, I'm going to donate all the snot I sneeze to hospitals for mucus transfusions." (Calvin and Hobbes) Luke Plant || L.Plant.98 (at) cantab.net || http://lukeplant.me.uk/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-developers -~----------~----~----~----~------~----~------~--~---
