django select_related for multiple foreign keys

dtc picture dtc · Jan 17, 2013 · Viewed 16.9k times · Source

How does select_related work with a model which has multiple foreign keys? Does it just choose the first one?

class Model: fkey1, fkey2, fkey3...

The documentation doesn't say anything about this, at least not in where the method is specified.

NOTE: Maybe I'm the only one who will get confused. I guess select_related is just a performance booster (I can see that) but I had the wrong idea that it was something else.

Answer

Chemical Programmer picture Chemical Programmer · Dec 24, 2015

You can use select_related in a chain as following

Comment.objects.select_related('user').select_related('article').all()