# different querysets for display and filters?

**URL:** <https://forum.djangoproject.com/t/different-querysets-for-display-and-filters/35421>\
**Category:** The Admin\
**Created:** [October 4, 2024, 7:03pm UTC](https://forum.djangoproject.com/t/different-querysets-for-display-and-filters/35421 "2024-10-04T19:03:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![newearthmartin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/newearthmartin/32/2020_2.png) [@newearthmartin](https://forum.djangoproject.com/u/newearthmartin)\
**Post date:** [October 4, 2024, 7:03pm UTC](https://forum.djangoproject.com/t/different-querysets-for-display-and-filters/35421/1 "2024-10-04T19:03:34Z")

</div>

Hi!

I have an admin for a model that has many items in `list_display`, including foreign key relationships and counts. I also have a couple of fields in `list_filter`.

I noticed that to display the list it made tons of queries, so I decided to optimize this by overriding `get_queryset` and adding `select_related` and `prefetch_related`. I managed to reduce hundreds of small queries into a single big query.

This is great for the 100 displayed models. However, it adds a ton of overhead for `list_filter` fields! `list_filter` works by going through all the objects in the table and collecting the different values. Since it is using the same queryset as the display, it is bringing all those relationships, doing all those joins, which are not really needed. So I realized that my great optimization makes for a worse performance ☹

I searched the documentation to see if there is a different queryset for display and filter, but I haven’t found anything. Am I stumbling on a problem that has been solved or is there some room to improve here?

---

<div class="post-metadata">

**Author:** ![philgyford](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/philgyford/32/14295_2.png) [@philgyford](https://forum.djangoproject.com/u/philgyford)\
**Post date:** [October 4, 2024, 7:11pm UTC](https://forum.djangoproject.com/t/different-querysets-for-display-and-filters/35421/2 "2024-10-04T19:11:28Z")

</div>

Would `list_selected_related` help avoid changing the queryset itself? [The Django admin site | Django documentation | Django](https://docs.djangoproject.com/en/5.1/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_select_related)

---

<div class="post-metadata">

**Author:** ![newearthmartin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/newearthmartin/32/2020_2.png) [@newearthmartin](https://forum.djangoproject.com/u/newearthmartin)\
**Post date:** [October 4, 2024, 7:32pm UTC](https://forum.djangoproject.com/t/different-querysets-for-display-and-filters/35421/3 "2024-10-04T19:32:42Z")

</div>

Yes this is helpful! Obviously not as expressive as having a custom queryset but it is something.

Is there a way to follow foreign key relationships? I used to do this with `prefetch_related`.

---

<div class="post-metadata">

**Author:** ![philgyford](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/philgyford/32/14295_2.png) [@philgyford](https://forum.djangoproject.com/u/philgyford)\
**Post date:** [October 4, 2024, 7:47pm UTC](https://forum.djangoproject.com/t/different-querysets-for-display-and-filters/35421/4 "2024-10-04T19:47:13Z")

</div>

You can follow foreign key relationships with `select_related` [QuerySet API reference | Django documentation | Django](https://docs.djangoproject.com/en/5.1/ref/models/querysets/#django.db.models.query.QuerySet.select_related)

Obviously it could result in slow queries depending on the size of your tables etc.

---

<div class="post-metadata">

**Author:** ![newearthmartin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/newearthmartin/32/2020_2.png) [@newearthmartin](https://forum.djangoproject.com/u/newearthmartin)\
**Post date:** [October 4, 2024, 7:48pm UTC](https://forum.djangoproject.com/t/different-querysets-for-display-and-filters/35421/5 "2024-10-04T19:48:38Z")

</div>

I mean, reverse foreign key… one to many relationships.  
As if my model is Book and I want to show the count of Page objects.

---

<div class="post-metadata">

**Author:** ![EverettBennett](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/everettbennett/32/24496_2.png) [@EverettBennett](https://forum.djangoproject.com/u/EverettBennett)\
**Post date:** [October 10, 2024, 6:13pm UTC](https://forum.djangoproject.com/t/different-querysets-for-display-and-filters/35421/6 "2024-10-10T18:13:33Z")

</div>

> [@newearthmartin](#):
>
> I have an admin for a model that has many items in `list_display`, including foreign key relationships and counts. I also have a couple of fields in `list_filter`.
> 
> I noticed that to display the list it made tons of queries, so I decided to optimize this by overriding `get_queryset` and adding `select_rel[a](https://gameroom777.download/)ted` and `prefetch_related`. I managed to reduce hundreds of small queries into a single big query.
> 
> This is great for the 100 displayed models. However, it adds a ton of overhead for `list_filter` fields! `list_filter` works by going through all the objects in the table and collecting the different values. Since it is using the same queryset as the display, it is bringing all those relationships, doing all those joins, which are not really needed. So I realized that my great optimization makes for a worse performance ☹
> 
> I searched the documentation to see if there is a different queryset for display and filter, but I haven’t found anything. Am I stumbling on a problem that has been solved or is there some room to improve here?

Have you explored using `only()` or `defer()` methods in your queryset to limit the fields fetched for `list_filter` while still optimizing the main display?

---

<div class="post-metadata">

**Author:** ![newearthmartin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/newearthmartin/32/2020_2.png) [@newearthmartin](https://forum.djangoproject.com/u/newearthmartin)\
**Post date:** [October 11, 2024, 3:48am UTC](https://forum.djangoproject.com/t/different-querysets-for-display-and-filters/35421/7 "2024-10-11T03:48:12Z")

</div>

> Have you explored using `only()` or `defer()` methods in your queryset to limit the fields fetched for `list_filter` while still optimizing the main display

No I haven’t tried it, it seems to me that the join will still happen but the fields won’t be retrieved?
