# Queryset optimization in Admin

**URL:** <https://forum.djangoproject.com/t/queryset-optimization-in-admin/3996>\
**Category:** Using Django\
**Created:** [August 20, 2020, 12:38pm UTC](https://forum.djangoproject.com/t/queryset-optimization-in-admin/3996 "2020-08-20T12:38:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sheraff0](https://avatars.discourse-cdn.com/v4/letter/s/dc4da7/32.png) [@sheraff0](https://forum.djangoproject.com/u/sheraff0)\
**Post date:** [August 20, 2020, 12:38pm UTC](https://forum.djangoproject.com/t/queryset-optimization-in-admin/3996/1 "2020-08-20T12:38:32Z")

</div>

```
class ProductAdmin(admin.ModelAdmin):
    ...
    def get_queryset(self, request):
        qs = super().get_queryset(request)
        qs = qs.select_related(
            'subcategory __category__ main_category'
        ).prefetch_related(
            'product_images',
            'product_in_stock',
            Prefetch('product_specs', queryset=ProductSpec.objects.prefetch_related('product_spec_key'))
        )
        return qs

```

This approach works terrific in views, but on admin page I’ve got even more database hits (200+ compared to 190+).  
Is it possible to optimize queryset in Admin in this way, perhaps with a bit more overriding of parent class?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 20, 2020, 1:04pm UTC](https://forum.djangoproject.com/t/queryset-optimization-in-admin/3996/2 "2020-08-20T13:04:34Z")

</div>

Jacinda Shelly gave a wonderful talk at PyCon last year titled, “But, Why is the admin slow?” - [https://youtu.be/f8cFjiyxQuQ](https://youtu.be/f8cFjiyxQuQ)

If you’re really interested in improving the performance of your admin pages, it’s well worth your time to watch it.

She’s really talking about a more general issue regarding performance improvements, but it’s remarkably useful for optimizing the queries being performed in the ModelAdmin classes.

Ken

---

<div class="post-metadata">

**Author:** ![sheraff0](https://avatars.discourse-cdn.com/v4/letter/s/dc4da7/32.png) [@sheraff0](https://forum.djangoproject.com/u/sheraff0)\
**Post date:** [August 20, 2020, 3:06pm UTC](https://forum.djangoproject.com/t/queryset-optimization-in-admin/3996/3 "2020-08-20T15:06:44Z")

</div>

Thank you Ken, I’ll check this one!

---

<div class="post-metadata">

**Author:** ![sheraff0](https://avatars.discourse-cdn.com/v4/letter/s/dc4da7/32.png) [@sheraff0](https://forum.djangoproject.com/u/sheraff0)\
**Post date:** [August 20, 2020, 4:21pm UTC](https://forum.djangoproject.com/t/queryset-optimization-in-admin/3996/4 "2020-08-20T16:21:06Z")

</div>

Looks like get\_queryset has to do with LISTS of objects but doesn’t have any impact on INDIVIDUAL objects in admin…  
I’ve got several Inlines and a ForeignKey selector with verbose ` __str__ ` as major culprits of slowdown.

---

<div class="post-metadata">

**Author:** ![sheraff0](https://avatars.discourse-cdn.com/v4/letter/s/dc4da7/32.png) [@sheraff0](https://forum.djangoproject.com/u/sheraff0)\
**Post date:** [August 20, 2020, 4:42pm UTC](https://forum.djangoproject.com/t/queryset-optimization-in-admin/3996/5 "2020-08-20T16:42:26Z")

</div>

One clue here: [formfield\_for\_foreignkey](https://docs.djangoproject.com/en/3.1/ref/contrib/admin/#django.contrib.admin.ModelAdmin.formfield_for_foreignkey)

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 21, 2020, 12:42pm UTC](https://forum.djangoproject.com/t/queryset-optimization-in-admin/3996/6 "2020-08-21T12:42:10Z")

</div>

Also take a look at [ModelAdmin.autocomplete\_fields](https://docs.djangoproject.com/en/3.1/ref/contrib/admin/#django.contrib.admin.ModelAdmin.autocomplete_fields). That’ll be helpful if you can’t filter the choices.

---

<div class="post-metadata">

**Author:** ![sheraff0](https://avatars.discourse-cdn.com/v4/letter/s/dc4da7/32.png) [@sheraff0](https://forum.djangoproject.com/u/sheraff0)\
**Post date:** [August 22, 2020, 12:25pm UTC](https://forum.djangoproject.com/t/queryset-optimization-in-admin/3996/7 "2020-08-22T12:25:50Z")

</div>

Thanks, great hint!  
The worst part is with inlines - they produce dozens of extra database queries, and the seem to be no way to reduce them to one query.
