On Fri, Jul 19, 2013 at 01:02:34PM +0100, Pablo Recio wrote: > Hi devs, > > I just came up with the following scenario: > > class A(models.Model): > # some fields... > b = models.ForeignKey(B) > > class B(models.Model): > # other fields > > A simple FK relationship. Well, then I wanted to fetch objects from 'A' > model but just for retrieve the related 'B', so I tried something like: > > A.objects.only('b').all() > > I hoped it was going to be smart enough to check that I'm accessing to FK > and then do 'select_related' for that field behind the scenes, but it > doesn't, so I needed to chain the select_related myself. > > Is there any reason for this behaviour? If not, would it be a good patch to > submit?
Explicit is better than implicit. Also, special cases aren't special enough to break the rules. To me, what you propose seems too arbitrary. `only` would do what it says it does, that is, defer everything but the selected fields, except for the special case where you restrict the query to a single ForeignKey, in which case it would also do a `select_related` -- that sounds like a quite specific special case. Also, what would you propose to happen in case you do `defer` on all fields except one ForeignKey? Would that do the same thing as the equivalent `only` or not? Why? In other words, I don't think it is worth complicating the API in this way just to save a single method call on a QuerySet. Michal
signature.asc
Description: Digital signature