# Additional order\_by id added by ModelAdmin

**URL:** <https://forum.djangoproject.com/t/additional-order-by-id-added-by-modeladmin/1308>\
**Category:** Using Django\
**Created:** [February 27, 2020, 11:57am UTC](https://forum.djangoproject.com/t/additional-order-by-id-added-by-modeladmin/1308 "2020-02-27T11:57:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lallulli](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/lallulli/32/744_2.png) [@lallulli](https://forum.djangoproject.com/u/lallulli)\
**Post date:** [February 27, 2020, 11:57am UTC](https://forum.djangoproject.com/t/additional-order-by-id-added-by-modeladmin/1308/1 "2020-02-27T11:57:39Z")

</div>

Hi!

I’m ordering a huge model, in its `ModelAdmin` class, using an `ordering` variable:

```python
class RevisionAdmin(admin.ModelAdmin):
    ordering = ('-created',)

```

However, the generated query orders by both fields `created` and `id`:

```sql
ORDER BY "dl_revision"."created" DESC, "dl_revision"."id" DESC

```

This causes a huge problem with Postgres optimizer: for some reasons, it does not use foreign key indexes to perform joins (`Revision` has two foreign key fields), but it makes a full table scan. If I manually remove ordering by `id` from the query, the optimizer is able to perform joins using indexes.

As a (inefficient) workaround, I created a further combined index with fields `('created', 'id')`, and the joins use indexes now.

My question is: is it possible to disable adding the `id` column to the `order by` clause, and stick with the fields explicitly requested only?

---

<div class="post-metadata">

**Author:** ![kplaube](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kplaube/32/629_2.png) [@kplaube](https://forum.djangoproject.com/u/kplaube)\
**Post date:** [February 28, 2020, 9:38am UTC](https://forum.djangoproject.com/t/additional-order-by-id-added-by-modeladmin/1308/2 "2020-02-28T09:38:40Z")

</div>

That’s the exact behaviour that Django’s documentation describes ([https://docs.djangoproject.com/en/3.0/ref/contrib/admin/#django.contrib.admin.ModelAdmin.ordering](https://docs.djangoproject.com/en/3.0/ref/contrib/admin/#django.contrib.admin.ModelAdmin.ordering)), and your workaround is kind of suggested there as well.

Have you tried overriding the `get_ordering` method? I haven’t tried, but by reading the docs and the code, it looks like you can achieve the expected outcome by doing that:

- [https://docs.djangoproject.com/en/3.0/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get\_ordering](https://docs.djangoproject.com/en/3.0/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_ordering)
- [https://github.com/django/django/blob/b457068cf272bc9912187a2b7aaaa4445110735e/django/contrib/admin/views/main.py#L289](https://github.com/django/django/blob/b457068cf272bc9912187a2b7aaaa4445110735e/django/contrib/admin/views/main.py#L289) (note the `_get_deterministic_ordering` call by the end of the method)

---

<div class="post-metadata">

**Author:** ![lallulli](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/lallulli/32/744_2.png) [@lallulli](https://forum.djangoproject.com/u/lallulli)\
**Post date:** [February 28, 2020, 5:08pm UTC](https://forum.djangoproject.com/t/additional-order-by-id-added-by-modeladmin/1308/3 "2020-02-28T17:08:02Z")

</div>

Thanks for all your info, now it is much clearer to me what happens behind the scene!

I’ve tried to override `get_ordering` of class `RevisionAdmin`:

```auto
    def get_ordering(self, request):
        return ['-created']

```

but the situation does not change: `id` is added to sorting fields by that `_get_deterministic_ordering_()` function that is called by the framework _after_ the call to the overridden `get_ordering`.

It looks like I should redefine the `get_ordering()` or `_get_deterministic_ordering()` method of class `ChangeView`… more precisely, I should define a new class, say `RevisionChangeView` that extends `ChangeView`, override its methods, and in some way tell `RevisionAdmin` to use `RevisionChangeView`. But I don’t know if it is possible…
