# Django ORM queryset, include index in values\_list

**URL:** <https://forum.djangoproject.com/t/django-orm-queryset-include-index-in-values-list/7031>\
**Category:** Using Django\
**Created:** [March 13, 2021, 10:37pm UTC](https://forum.djangoproject.com/t/django-orm-queryset-include-index-in-values-list/7031 "2021-03-13T22:37:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![devspectre](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/devspectre/32/3559_2.png) [@devspectre](https://forum.djangoproject.com/u/devspectre)\
**Post date:** [March 13, 2021, 10:37pm UTC](https://forum.djangoproject.com/t/django-orm-queryset-include-index-in-values-list/7031/1 "2021-03-13T22:37:35Z")

</div>

Hi

I have a queryset looks like this.  
toys = Toy.objects.filter(…).order\_by(‘price’, ‘id’).values\_list(‘id’, ‘price’, ‘name’)

So the tuples inside the queryset look like this  
(1, 35, ‘Woody’), (2, 45, ‘E-car’)

I want to add another value which is actually order of sorted result like this.  
(1, 35, ‘Woody’, 0), (2, 45, ‘E-car’, 1), (13, 56, ‘G-toy’, 2)

How can I do this in queryset?

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:** [March 14, 2021, 12:51am UTC](https://forum.djangoproject.com/t/django-orm-queryset-include-index-in-values-list/7031/2 "2021-03-14T00:51:44Z")

</div>

Django has the [RowNumber](https://docs.djangoproject.com/en/3.1/ref/models/database-functions/#rownumber) function which can be used to annotate a result set. It’s a window function making it a little “fiddly” to use (my opinion), but there are samples floating around that might point you in the right direction. (I think it’s going to be something along the lines of:  
`Toy.objects.filter(...).annotate(row_number=Window(expression=RowNumber(), order_by=F('price'),)).order_by('price')`

You can also augment your result set when you iterate over those results if you don’t want to do it in the queryset.

---

<div class="post-metadata">

**Author:** ![devspectre](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/devspectre/32/3559_2.png) [@devspectre](https://forum.djangoproject.com/u/devspectre)\
**Post date:** [March 14, 2021, 1:35am UTC](https://forum.djangoproject.com/t/django-orm-queryset-include-index-in-values-list/7031/3 "2021-03-14T01:35:47Z")

</div>

Hi, @KenWhitesell

Thank you for your answer.  
It works, that’s exactly what I wanted.

😀
