On Jan 7, 5:07 am, apramanik <abh...@gmail.com> wrote:
> Oops not a join, but still a db hit :).
>
> On Jan 6, 9:03 pm, apramanik <abh...@gmail.com> wrote:
>
> > Hey all, if I have a foreign key on a model (say Model B has a foreign
> > key to Model A) and I do something like this:
>
> > a = A.objects.get( pk = 1 )
> > b = B.objects.get( pk = 1 )
>
> > if b.a == a :
> >     # Do something
>
> > Is that inefficient, since its doing a join to pull b.a? Should I be
> > comparing their ids? How do I get that from the b model? Something
> > like this:
>
> > if b.a_id == a.id :
> >    # Do something
>
> > Not sure about the syntax. Thanks!


If you had done
    b = B.objects.select_related().get(pk=1)
that would have done a join, so you would incur some penalty there but
you wouldn't get a separate db hit when you do b.a.

However, your alternative syntax is correct, and as long as you don't
need to access anything else on b.a it will save you the extra cost.
--
DR.
-- 
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