# Is it possible to order the Admin panel by a FK?

**URL:** <https://forum.djangoproject.com/t/is-it-possible-to-order-the-admin-panel-by-a-fk/21483>\
**Category:** The Admin\
**Created:** [June 9, 2023, 3:37pm UTC](https://forum.djangoproject.com/t/is-it-possible-to-order-the-admin-panel-by-a-fk/21483 "2023-06-09T15:37:11Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![swills1](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/swills1/32/6475_2.png) [@swills1](https://forum.djangoproject.com/u/swills1)\
**Post date:** [June 9, 2023, 3:37pm UTC](https://forum.djangoproject.com/t/is-it-possible-to-order-the-admin-panel-by-a-fk/21483/1 "2023-06-09T15:37:11Z")

</div>

I use `ordering = ['...']` to sort my model data within the admin panel.

Is it possible to sort by a foreignKey?

Thanks.

---

<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:** [June 9, 2023, 4:01pm UTC](https://forum.djangoproject.com/t/is-it-possible-to-order-the-admin-panel-by-a-fk/21483/2 "2023-06-09T16:01:18Z")

</div>

Have you tried it?

Also, see the docs for [list\_display](https://docs.djangoproject.com/en/4.2/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_display).

---

<div class="post-metadata">

**Author:** ![swills1](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/swills1/32/6475_2.png) [@swills1](https://forum.djangoproject.com/u/swills1)\
**Post date:** [June 9, 2023, 4:16pm UTC](https://forum.djangoproject.com/t/is-it-possible-to-order-the-admin-panel-by-a-fk/21483/3 "2023-06-09T16:16:40Z")

</div>

You’re asking if I’ve tried something I don’t know the steps needed to try?

Also, if I tried and succeded, why ask?

Yes, I tried random things with zero insight. I am here seeking insight on how to achieve.

I will look at the link.

---

<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:** [June 9, 2023, 4:18pm UTC](https://forum.djangoproject.com/t/is-it-possible-to-order-the-admin-panel-by-a-fk/21483/4 "2023-06-09T16:18:40Z")

</div>

> [@swills1](#):
>
> Also, if I tried and succeded, why ask?

Because you might have tried and failed - in which case it would be extremely useful to see what you tried, and then to figure out what why it didn’t work.

---

<div class="post-metadata">

**Author:** ![swills1](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/swills1/32/6475_2.png) [@swills1](https://forum.djangoproject.com/u/swills1)\
**Post date:** [June 9, 2023, 4:22pm UTC](https://forum.djangoproject.com/t/is-it-possible-to-order-the-admin-panel-by-a-fk/21483/5 "2023-06-09T16:22:31Z")

</div>

No, that only leads us down the road of trying to cram in possibly flawed method. It is much better to start again and ask for the proper method. This situation isn’t trying to fix code logic, it is about finding out the proper methd to achieve something built into django.

`list_display` isn’t what I am asking about. I am asking about ordering the model data. Which is done with `ordering = ['..']`.

list\_display lets you add columns. I am talking abut sorting the data within the columns.

---

<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:** [June 9, 2023, 4:23pm UTC](https://forum.djangoproject.com/t/is-it-possible-to-order-the-admin-panel-by-a-fk/21483/6 "2023-06-09T16:23:31Z")

</div>

And if you read through that section, you’ll see that it discusses the use of the `ordering` attribute and how it applies to the `list_display`, and what all you can do with that attribute.

---

<div class="post-metadata">

**Author:** ![swills1](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/swills1/32/6475_2.png) [@swills1](https://forum.djangoproject.com/u/swills1)\
**Post date:** [June 9, 2023, 4:55pm UTC](https://forum.djangoproject.com/t/is-it-possible-to-order-the-admin-panel-by-a-fk/21483/7 "2023-06-09T16:55:38Z")

</div>

Here is the solution. I just found it on SO and tested.  
I decided to search for - show foreign key in list display. Because I knew I’d get more hits and once I had the FK in my list\_display - I could order by it.

```auto
class QuotesAdmin(admin.ModelAdmin):
    list_display = ('quote_id', 'mission', 'get_mission')
    @admin.display(ordering='missions__mission_no', description='Column Title')
    def get_mission(self, obj):
        return obj.mission.mission_no

```

> <https://stackoverflow.com/questions/163823/can-list-display-in-a-django-modeladmin-display-attributes-of-foreignkey-field>

What got me at first was - on the last line - it wants the column / variable. Not the model Class name.

---

<div class="post-metadata">

**Author:** ![strokirk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/strokirk/32/3414_2.png) [@strokirk](https://forum.djangoproject.com/u/strokirk)\
**Post date:** [June 10, 2023, 10:38am UTC](https://forum.djangoproject.com/t/is-it-possible-to-order-the-admin-panel-by-a-fk/21483/8 "2023-06-10T10:38:33Z")

</div>

You’ve already found a solution, but I’d just like to chime in and say that you can add this sorting to any custom field in the same way, even sorting on a column of the foreign key relationship. Basically, anything you can stuff inside ”order\_by”.

If you need to sort by non-column data, like things produced by ”annotate”, that’s also possible.
