# Help! Facing error while ordering qs!

**URL:** <https://forum.djangoproject.com/t/help-facing-error-while-ordering-qs/41543>\
**Category:** Using the ORM\
**Created:** [June 23, 2025, 3:01pm UTC](https://forum.djangoproject.com/t/help-facing-error-while-ordering-qs/41543 "2025-06-23T15:01:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Manpreet-Singh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/manpreet-singh/32/26728_2.png) [@Manpreet-Singh](https://forum.djangoproject.com/u/Manpreet-Singh)\
**Post date:** [June 23, 2025, 3:01pm UTC](https://forum.djangoproject.com/t/help-facing-error-while-ordering-qs/41543/1 "2025-06-23T15:01:09Z")

</div>

Hey,

Below is my implemented code to order my queryset on behalf of the reverse related fields but I am getting error if I’m using distinct, I am bounded to use distinct as I want unique services only which must be ordered by given argument in order by.

```auto
filtered_qs = service_filter.qs
ordered_qs = filtered_qs.order_by("-my_services __my_experience__ no_of_years_of_experience")
distinct_qs = ordered_qs.distinct("service_id")

```

but facing the below error:

`psycopg2.errors.InvalidColumnReference: SELECT DISTINCT ON expressions must match initial ORDER BY expressions`

Your help would be reallly appreciated.

I am trying to implement dynamic sorting or ordering with FilterSet - Ordering Filter.

Thanks in advance!

---

<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 23, 2025, 3:05pm UTC](https://forum.djangoproject.com/t/help-facing-error-while-ordering-qs/41543/2 "2025-06-23T15:05:39Z")

</div>

> [@Manpreet-Singh](#):
>
> psycopg2.errors.InvalidColumnReference: SELECT DISTINCT ON expressions must match initial ORDER BY expressions

I think this pretty much explains the issue and what must be done to correct this.

Whatever you use in the `distinct` clause must be the leading elements of the `order_by` clause.

See the second Note box in the docs at [`distinct`](https://docs.djangoproject.com/en/5.2/ref/models/querysets/#distinct).

---

<div class="post-metadata">

**Author:** ![Manpreet-Singh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/manpreet-singh/32/26728_2.png) [@Manpreet-Singh](https://forum.djangoproject.com/u/Manpreet-Singh)\
**Post date:** [June 23, 2025, 3:09pm UTC](https://forum.djangoproject.com/t/help-facing-error-while-ordering-qs/41543/3 "2025-06-23T15:09:04Z")

</div>

I understood that well but but but, What if I am having a requirement to implement the same, because as per the tagged forum shared by @KenWhitesell I am bounded to use my\_services\_\_my\_experience\_\_no\_of\_years\_of\_experience as distinct but I need the distinct service id instead, I hope you got me there?

---

<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 23, 2025, 3:49pm UTC](https://forum.djangoproject.com/t/help-facing-error-while-ordering-qs/41543/4 "2025-06-23T15:49:03Z")

</div>

I understand what you _want_. However, the orm requires that you specify whatever fields are in the `distinct` clause be used as the first fields in the `order_by` clause. See the examples in the referenced docs.

Basically, what this means is that you may need to handle some of this logic within your code - that the orm may not strictly be able to retrieve the results you want in the order that you want them.
