Retrieve all related objects (reverse foreign keys) efficiently with django
<p>I have the following models:</p>

<pre><code>Car:
    name = models.CharField(max_length=50)
    model = models.CharField(max_length=80)

Driver:
    name = models.CharField(max_length=50)
    address = models.CharField(max_length=80)
    car = models.ForeignKey(Car, on_delete=models.CASCADE, related_name='drivers')
</code></pre>

<p>Now I want to retrieve all of cars including related model driver data</p>

<pre><code>cars = Car.objects.prefetch_related('drivers').all()
</code></pre>

<p>Now in my template I just want to print each car and the first driver information</p>

<pre><code>{% for car in cars %}
  &lt;tr&gt;
    &lt;td&gt;car.name&lt;/td&gt;
    &lt;td&gt;car.model&lt;/td&gt;
    &lt;td&gt;car.drivers.first.name&lt;/td&gt;
    &lt;td&gt;car.drivers.first.address&lt;/td&gt;
  &lt;/tr&gt;
{% endfor %}
</code></pre>

<p>However, this results in 2k+ queries for around 500 cards in my db and it takes a while to load. Am I doing something wrong, is there anyway to make this more efficient?</p>