<p>What's the best and/or fastest method of doing multijoin queries in Django using the ORM and QuerySet API? </p>

[{'answer_id': 73349, 'author': 'Aaron', 'author_id': 11176, 'author_profile': 'https://Stackoverflow.com/users/11176', 'pm_score': 2, 'selected': False, 'text': '<p>If you are trying to join across tables linked by ForeignKeys or ManyToManyField relationships then you can use the double underscore syntax. For example if you have the following models:</p>\n\n<pre><code>class Foo(models.Model):\n    name = models.CharField(max_length=255)\n\nclass FizzBuzz(models.Model):\n    bleh = models.CharField(max_length=255)\n\nclass Bar(models.Model):\n    foo = models.ForeignKey(Foo)\n    fizzbuzz = models.ForeignKey(FizzBuzz) \n</code></pre>\n\n<p>You can do something like:</p>\n\n<pre><code>Fizzbuzz.objects.filter(bar__foo__name = "Adrian")\n</code></pre>\n'}, {'answer_id': 73462, 'author': 'machineghost', 'author_id': 5921, 'author_profile': 'https://Stackoverflow.com/users/5921', 'pm_score': 2, 'selected': False, 'text': '<p>Don\'t use the API ;-)  Seriously, if your JOIN are complex, you should see significant performance increases by dropping down in to SQL rather than by using the API.  And this doesn\'t mean you need to get dirty dirty SQL all over your beautiful Python code; just make a custom manager to handle the JOINs and then have the rest of your code use it rather than direct SQL.</p>\n\n<p>Also, I was just at DjangoCon where they had a seminar on high-performance Django, and one of the key things I took away from it was that if performance is a real concern (and you plan to have significant traffic someday), you really shouldn\'t be doing JOINs in the first place, because they make scaling your app while maintaining decent performance virtually impossible.</p>\n\n<p>Here\'s a video Google made of the talk:\n<a href="http://www.youtube.com/watch?v=D-4UN4MkSyI&amp;feature=PlayList&amp;p=D415FAF806EC47A1&amp;index=20" rel="nofollow noreferrer">http://www.youtube.com/watch?v=D-4UN4MkSyI&amp;feature=PlayList&amp;p=D415FAF806EC47A1&amp;index=20</a></p>\n\n<p>Of course, if you know that your application is never going to have to deal with that kind of scaling concern, JOIN away :-)  And if you\'re also not worried about the performance hit of using the API, then you really don\'t need to worry about the (AFAIK) miniscule, if any, performance difference between using one API method over another.</p>\n\n<p>Just use:\n<a href="http://docs.djangoproject.com/en/dev/topics/db/queries/#lookups-that-span-relationships" rel="nofollow noreferrer">http://docs.djangoproject.com/en/dev/topics/db/queries/#lookups-that-span-relationships</a></p>\n\n<p>Hope that helps (and if it doesn\'t, hopefully some true Django hacker can jump in and explain why method X actually does have some noticeable performance difference).</p>\n'}, {'answer_id': 372079, 'author': 'bnjmnhggns', 'author_id': 35254, 'author_profile': 'https://Stackoverflow.com/users/35254', 'pm_score': 1, 'selected': False, 'text': "<p>Use the queryset.query.join method, but only if the other method described here (using double underscores) isn't adequate.</p>\n"}, {'answer_id': 1894381, 'author': 'Viesturs', 'author_id': 1660, 'author_profile': 'https://Stackoverflow.com/users/1660', 'pm_score': 0, 'selected': False, 'text': '<p>Caktus blog has an answer to this: <a href="http://www.caktusgroup.com/blog/2009/09/28/custom-joins-with-djangos-queryjoin/" rel="nofollow noreferrer">http://www.caktusgroup.com/blog/2009/09/28/custom-joins-with-djangos-queryjoin/</a></p>\n\n<p>Basically there is a hidden QuerySet.query.join method that allows adding custom joins.</p>\n'}]
['https://Stackoverflow.com/questions/72568', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']