No.

select_related() will work on ForeignKey() relationships and OneToOneField()
relationships and reverse OneToOneField() relationships.

Example:

class Blog(models.Model):

    title = models.CharField()


> class Entry(models.Model):

    body = models.TextField()

    blog = models.ForeignKey(Blog)


> class Author(models.Model):

    name = models.CharField()

    blog = models.OneToOneField(Blog)


> Entry.objects.select_related('blog').all() #works

Author.objects.select_related('blog').all() #works

Blog.objects.select_related('author').all() #works

Blog.objects.select_related('entry_set').all() #doesn't work; you can't
> follow the reverse ForeignKey()


Basically, select_related() works when you'll only get one related item.  A
blog can have multiple entries so you can't get the multiple entries.  A
blog only has one author so you can get the author.  An author only has one
blog (in this example) so you can get the blog.  An entry is only in one
blog so you can get the blog.  Malcom can comment on this further, but
basically it has to do with the complexity of multiple select_related()
joins.

http://www.mail-archive.com/django-users@googlegroups.com/msg71662.html -
you can wade through that thread if you're interested in learning more.
 It's doable and rather than doing it as a join, an IN() query would
probably work nicely.  With a join (which is how select_related() currently
works), you get a lot of duplicate data.

An interesting thing to look at would be Ruby's DataMapper.  Not only does
it avoid the join problem when fetching related rows, but it doesn't need to
be told to select related data - it does it automatically (see strategic
eager loading: http://datamapper.org/why).  Basically, it's smart enough to
realize when you're calling a relation method on an object that was part of
a list/Array (depending on whether you like the Python or Ruby term) and
that it should actually hit the database once for all the items in that list
and only make one query for all the objects in that list.

Anyway, that's probably way more detail than you wanted.  If you have
questions, don't hesitate to ask.

Sean.






On Thu, Sep 9, 2010 at 8:49 PM, Winnie <vin...@gmail.com> wrote:
> Does select_related() work if used on a ManyToMany field?
>
> --
> 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<django-users%2bunsubscr...@googlegroups.com>
.
> For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.
>
>

-- 
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 at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to