Hi all,

I'm seeing some funny behavior when using select_related to try to speed up
my code, and I'm wondering if I'm just misunderstanding something.

The basics of my code:

class A(models.Model):
    # some stuff

class B(models.Model):
    a = models.ForeignKey('A')
    c = models.ForeignKey('C')
    d = models.ForeignKey('D')
    e = models.ForeignKey('E')
    # some stuff

a = A.objects.get(pk=some_id)
b_set = a.b_set.all()

When I do this, b_set has about 8 things in it, and I can get to c, d, e,
and other things just fine.  But, presumably, accessing b.c, b.d, and b.e
are each database calls, and I would like to avoid those if I can.

So to get rid of those database calls and just get all of the related fields
in a single query, I do this:

a = A.objects.get(pk=some_id)
b_set = a.b_set.select_related().all() # recommended in the Django
documentation for some queries

And b_set comes up empty, for some reason that I can't explain.  The only
difference between the two blocks of code is the select_related() call, and
it changes the result by not giving me anything back.  Any ideas?

Thanks,
Matt

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@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