[Django]-How to combine django "prefetch_related" and "values" methods?

9👍

Use only() to limit number of fields retrieved if you’re concerned about your app performances. See reference.

In the example above, this would be:

Organizations.objects.prefetch_related('locations').only('id', 'name').order_by('name')

which would result in two queries:

SELECT id, name FROM organizations;
SELECT * from locations WHERE organization_name = <whatever is seen before>;
👤Antwan

Leave a comment