On Fri, 2007-05-11 at 08:13 -0700, [EMAIL PROTECTED] wrote: > Hello again DjangoUsers, > > Thank you for your reply Malcolm, actually my problem remained in the > fact my own object name was not "Restaurant" but something like > "ItalianRestaurant", and I only tried to call it from places by > "italianRestaurant", as I would have done in Java reflect for example, > I didn't notice it has to be full lowercase like "italianrestaurant" > to work. > > So this works as intended, but it hasn't changed the fact that around > 2 thousands connexions are needed to do the request, instead of one. > > I do the following : > > for place in Place.objects.select_related() : > try : > restaurant = place.restaurant > except ObjectDoesNotExist : > pass > > > I got the same number of connexions when trying : > > for place in Place.objects.all() : > try : > restaurant = place.restaurant > except ObjectDoesNotExist : > pass > > > Why does it hit the database when I write "select_related()" ? > Postgres logs tell me it hits the database each time I call > "place.restaurant", whether I'd written "select_related" or not.
The short answer is because select_related() does not work across reverse links (the Place -> Restaurant direction). It also won't save you any requests when model inheritance is implemented. The longer answer is that if you know you want to work with the Restaurant objects, it is better to request them directly (and use select_related()), rather than requesting the least-specific object and trying to specialise. If you think about things in the model inheritance type of situation (which OneToOne emulates fairly well), this will make more sense: you could have many different tables links to the Place "parent" model. So if you are querying Place objects you apparently want to work with the lowest common denominator fields and only specialise on demand. Otherwise, we would need to query across a potentially very large number of tables (including all sub-sub-classes, etc) to get everything. Regards, Malcolm --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" 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-users?hl=en -~----------~----~----~----~------~----~------~--~---

