Re: Making ".only()" a bit smarter

2013-07-23 Thread Pablo Recio
Yeah, you might be right. It just feels weird that if I'm trying to fetch
*only* a FK, it needs to do a second lookup. But explicit is better than
implicit indeed.


On 19 July 2013 21:48, Michal Petrucha  wrote:

> 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
>



-- 
Pablo Recio Quijano

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
For more options, visit https://groups.google.com/groups/opt_out.




Re: Making ".only()" a bit smarter

2013-07-19 Thread Michal Petrucha
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