Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » select_related() depth when specifying fields [RSS Feed]

#1 June 11, 2010 00:09:06

Michael H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

select_related() depth when specifying fields


If I use select_related() on a queryset and specify some attribute names, will
it follow those relationships as far as possible, or does that imply
"depth=1"?

Thanks,
Michaelsignature.ascDescription:This is a digitally signed message part.

Attachments:
attachment signature.asc (197 bytes)

Offline

#2 June 11, 2010 00:10:46

Michael H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

select_related() depth when specifying fields


I read that myself. I asked the question because it goes on to state that you
can specify down-stream models like this:

query.select_related(person__place)

If it follows all relationships as far as possible, even when specific
relationships are requested, why would that syntax ever be necessary? We
could just specify "people", and it would follow the "place" relationship
automatically. Is it only to specifically prevent it from following other
relationships on the "person" model?

The docs seem to be ambiguous on this point.

Thanks,
Michael




On Thursday 10 June 2010 03:37:59 pm Dan Harris wrote:
> According to the select_related() documentation at:
>http://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models> .QuerySet.select_related
>
> it looks like select related "follows foreign keys as far as
> possible". In the documentation example it shows select_related
> following a foreign keys with a depth of two.
>
> Hope this helps,
>
> Dan Harris
> dih0...@gmail.com
>
> On Jun 10, 3:19 pm, Michael Hrivnak <mhriv...@americanri.com> wrote:
> > If I use select_related() on a queryset and specify some attribute names,
> > will it follow those relationships as far as possible, or does that imply
> > "depth=1"?
> >
> > Thanks,
> > Michael
> >
> > signature.asc
> > < 1KViewDownload
>signature.ascDescription:This is a digitally signed message part.

Attachments:
attachment signature.asc (197 bytes)

Offline

#3 June 11, 2010 00:15:08

Dan H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

select_related() depth when specifying fields


According to the select_related() documentation at:http://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.QuerySet.select_relatedit looks like select related "follows foreign keys as far as
possible". In the documentation example it shows select_related
following a foreign keys with a depth of two.

Hope this helps,

Dan Harris
dih0...@gmail.com

On Jun 10, 3:19 pm, Michael Hrivnak <mhriv...@americanri.com> wrote:
> If I use select_related() on a queryset and specify some attribute names, will
> it follow those relationships as far as possible, or does that imply
> "depth=1"?
>
> Thanks,
> Michael
>
>  signature.asc
> < 1KViewDownload

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 June 11, 2010 00:20:41

Dan H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

select_related() depth when specifying fields


Michael,

I believe the case of query.select_related(person__place) will "only"
follow the place foreign key. This means that if you had 5 FK's in
your person models, that only a single FK would be followed instead of
all 5 for optimization. Upon further looking there is also a "depth"
argument you can pass to select related to specify how far you want it
to go. For example:

query.select_related(depth=5) # This will follow all FK's 5 levels
deep

query.select_ralted('person__place', depth=5) # This will follow only
the place FK 5 levels deep

That is how I read the docs, it may not be correct :)

Cheers,

Dan Harris
dih0...@gmail.com

On Jun 10, 4:54 pm, Michael Hrivnak <mhriv...@americanri.com> wrote:
> I read that myself.  I asked the question because it goes on to state that you
> can specify down-stream models like this:
>
> query.select_related(person__place)
>
> If it follows all relationships as far as possible, even when specific
> relationships are requested, why would that syntax ever be necessary?  We
> could just specify "people", and it would follow the "place" relationship
> automatically.  Is it only to specifically prevent it from following other
> relationships on the "person" model?
>
> The docs seem to be ambiguous on this point.
>
> Thanks,
> Michael
>
> On Thursday 10 June 2010 03:37:59 pm Dan Harris wrote:
>
>
>
> > According to the select_related() documentation at:
> >http://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db....
> > .QuerySet.select_related
>
> > it looks like select related "follows foreign keys as far as
> > possible". In the documentation example it shows select_related
> > following a foreign keys with a depth of two.
>
> > Hope this helps,
>
> > Dan Harris
> > dih0...@gmail.com
>
> > On Jun 10, 3:19 pm, Michael Hrivnak <mhriv...@americanri.com> wrote:
> > > If I use select_related() on a queryset and specify some attribute names,
> > > will it follow those relationships as far as possible, or does that imply
> > > "depth=1"?
>
> > > Thanks,
> > > Michael
>
> > >  signature.asc
> > > < 1KViewDownload
>
>
>
>  signature.asc
> < 1KViewDownload

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#5 June 11, 2010 17:41:50

Michael H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

select_related() depth when specifying fields


I really appreciate your help Dan, but I don't think the answer to my question
is in the docs. I did my due diligence there before asking.

Regarding your suggestion, from the docs: "It's an error to use both a list of
fields and the depth parameter in the same select_related() call, since they
are conflicting options."

Anyone else have insight? Otherwise I guess I'll have to start reading
through the code.

Thanks,
Michael

On Thursday 10 June 2010 05:01:52 pm Dan Harris wrote:
> Michael,
>
> I believe the case of query.select_related(person__place) will "only"
> follow the place foreign key. This means that if you had 5 FK's in
> your person models, that only a single FK would be followed instead of
> all 5 for optimization. Upon further looking there is also a "depth"
> argument you can pass to select related to specify how far you want it
> to go. For example:
>
> query.select_related(depth=5) # This will follow all FK's 5 levels
> deep
>
> query.select_ralted('person__place', depth=5) # This will follow only
> the place FK 5 levels deep
>
> That is how I read the docs, it may not be correct :)
>
> Cheers,
>
> Dan Harris
> dih0...@gmail.com
>
> On Jun 10, 4:54 pm, Michael Hrivnak <mhriv...@americanri.com> wrote:
> > I read that myself. I asked the question because it goes on to state
> > that you can specify down-stream models like this:
> >
> > query.select_related(person__place)
> >
> > If it follows all relationships as far as possible, even when specific
> > relationships are requested, why would that syntax ever be necessary? We
> > could just specify "people", and it would follow the "place" relationship
> > automatically. Is it only to specifically prevent it from following
> > other relationships on the "person" model?
> >
> > The docs seem to be ambiguous on this point.
> >
> > Thanks,
> > Michael
> >
> > On Thursday 10 June 2010 03:37:59 pm Dan Harris wrote:
> > > According to the select_related() documentation at:
> > >http://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db....
> > > .QuerySet.select_related
> > >
> > > it looks like select related "follows foreign keys as far as
> > > possible". In the documentation example it shows select_related
> > > following a foreign keys with a depth of two.
> > >
> > > Hope this helps,
> > >
> > > Dan Harris
> > > dih0...@gmail.com
> > >
> > > On Jun 10, 3:19 pm, Michael Hrivnak <mhriv...@americanri.com> wrote:
> > > > If I use select_related() on a queryset and specify some attribute
> > > > names, will it follow those relationships as far as possible, or does
> > > > that imply "depth=1"?
> > > >
> > > > Thanks,
> > > > Michael
> > > >
> > > > signature.asc
> > > > < 1KViewDownload
> >
> > signature.asc
> > < 1KViewDownload
>signature.ascDescription:This is a digitally signed message part.

Attachments:
attachment signature.asc (197 bytes)

Offline

  • Root
  • » Django
  • » select_related() depth when specifying fields [RSS Feed]

Board footer

Moderator control

Enjoy the 18th of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support