# Next Row of a Queryset...

**URL:** <https://forum.djangoproject.com/t/next-row-of-a-queryset/24094>\
**Category:** Using the ORM\
**Created:** [September 24, 2023, 5:09pm UTC](https://forum.djangoproject.com/t/next-row-of-a-queryset/24094 "2023-09-24T17:09:13Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![doc1977](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/doc1977/32/13416_2.png) [@doc1977](https://forum.djangoproject.com/u/doc1977)\
**Post date:** [September 24, 2023, 5:09pm UTC](https://forum.djangoproject.com/t/next-row-of-a-queryset/24094/1 "2023-09-24T17:09:13Z")

</div>

Hi again folks,

at the moment I am struggeling with the question how to get next/previous row of a given queryset (while implementing NEXT/PREVIOUS buttons in Template)

If I work with this:

```auto
employee_qs = Employees.objects.filter(id=employee).order_by('last_name')

```

I could easily do the task by

```auto
employee = int(employee) + 1

or alternatively

employee = int(employee) - 1

```

(of course there would be need of error handling, if there is no higher or lower number but its only for show)

BUT I would like to switch to next/previous dataset in the SORTED queryset. So i don’t have to deal with the `id`, but with the `last_name` entry…

---

<div class="post-metadata">

**Author:** ![charettes](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/charettes/32/27_2.png) [@charettes](https://forum.djangoproject.com/u/charettes)\
**Post date:** [September 24, 2023, 5:23pm UTC](https://forum.djangoproject.com/t/next-row-of-a-queryset/24094/2 "2023-09-24T17:23:43Z")

</div>

Hey @doc1977! I suggest you have a look at [this answer](https://forum.djangoproject.com/t/how-to-get-the-next-object-by-id-in-detailview/23574/3) which suggests using `Subquery` for that.

---

<div class="post-metadata">

**Author:** ![CodenameTim](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/codenametim/32/31507_2.png) [@CodenameTim](https://forum.djangoproject.com/u/CodenameTim)\
**Post date:** [September 24, 2023, 5:34pm UTC](https://forum.djangoproject.com/t/next-row-of-a-queryset/24094/3 "2023-09-24T17:34:53Z")

</div>

Another approach is to select page+1 objects from the database then render the next button if there was Page+1 returned versus a lesser amount.

---

<div class="post-metadata">

**Author:** ![doc1977](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/doc1977/32/13416_2.png) [@doc1977](https://forum.djangoproject.com/u/doc1977)\
**Post date:** [September 26, 2023, 5:54am UTC](https://forum.djangoproject.com/t/next-row-of-a-queryset/24094/4 "2023-09-26T05:54:16Z")

</div>

Hi there and thx for the input. My problem is, that I DON’t want to deal with the `id`’s but with the alphabetically sorted entry for `last_name`

So if my current entry wohuld have id=5, the next entry in the sorted list of `last_name`s could have id=1 or id= 355…

Main problem to me: How can i discover the next/previous row in the sorted list of `last_name`s ?

---

<div class="post-metadata">

**Author:** ![CodenameTim](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/codenametim/32/31507_2.png) [@CodenameTim](https://forum.djangoproject.com/u/CodenameTim)\
**Post date:** [September 26, 2023, 10:12am UTC](https://forum.djangoproject.com/t/next-row-of-a-queryset/24094/5 "2023-09-26T10:12:42Z")

</div>

The concepts charettes and I proposed are the same. You can use a Subquery to grab the previous and next last name. Give those docs a look and play around with the ORM in a shell / REPL.

The other approach is still valid too. Increase your page size by one so you select one more to determine if there’s a next page.

Side note about sorting on last name. It’s unlikely to be unique so you need to add other sorting keys so that you end up with an fixed order. So maybe the second sort key is an id type field?

---

<div class="post-metadata">

**Author:** ![CodenameTim](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/codenametim/32/31507_2.png) [@CodenameTim](https://forum.djangoproject.com/u/CodenameTim)\
**Post date:** [September 26, 2023, 10:16am UTC](https://forum.djangoproject.com/t/next-row-of-a-queryset/24094/6 "2023-09-26T10:16:05Z")

</div>

Another aside, I think DRFs CursorPagination docs could be helpful to you [Pagination - Django REST framework](https://www.django-rest-framework.org/api-guide/pagination/#cursorpagination)

---

<div class="post-metadata">

**Author:** ![doc1977](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/doc1977/32/13416_2.png) [@doc1977](https://forum.djangoproject.com/u/doc1977)\
**Post date:** [September 28, 2023, 6:33am UTC](https://forum.djangoproject.com/t/next-row-of-a-queryset/24094/7 "2023-09-28T06:33:13Z")

</div>

> So maybe the second sort key is an id type field?

I was already thinking about implementing kind of `alphabetical_id`field in model an have this populated with ascending integers in the alphabetically sorted list. Afterwards one could easily use this integer values to navigate through NEXT/PREVIOUS by simply incrementing or decrementing this value. This would make it necessary to rewrite this index with every create or delete process within the databse entries, however. And here I am thinking of perfromance in the end, wether this is not a BAD idea in the end…

---

<div class="post-metadata">

**Author:** ![CodenameTim](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/codenametim/32/31507_2.png) [@CodenameTim](https://forum.djangoproject.com/u/CodenameTim)\
**Post date:** [September 28, 2023, 10:02am UTC](https://forum.djangoproject.com/t/next-row-of-a-queryset/24094/8 "2023-09-28T10:02:50Z")

</div>

I’m not sure I follow you. However, integers sorted as strings is rarely a good idea. Humans never expect that.

---

<div class="post-metadata">

**Author:** ![doc1977](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/doc1977/32/13416_2.png) [@doc1977](https://forum.djangoproject.com/u/doc1977)\
**Post date:** [October 1, 2023, 11:13am UTC](https://forum.djangoproject.com/t/next-row-of-a-queryset/24094/9 "2023-10-01T11:13:06Z")

</div>

After all my core problem is still:

Take for example:

```auto
Model.objects.all().order_by('last_name')

```

no use

```auto
Model.objects.filter(id=request.POST('id'))

```

to show data in template.

How can i discover the previous/next dataset in a `.oder_by('last_name')` queryset starting from the filtered query based on `id`?

---

<div class="post-metadata">

**Author:** ![doc1977](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/doc1977/32/13416_2.png) [@doc1977](https://forum.djangoproject.com/u/doc1977)\
**Post date:** [October 1, 2023, 1:18pm UTC](https://forum.djangoproject.com/t/next-row-of-a-queryset/24094/10 "2023-10-01T13:18:10Z")

</div>

Ok, tried it now this way and it seems to work:

```auto
def prev_next(employee, request):

    unique_id = Employees.objects.get(id=employee).employee_number
    id_list = list(Employees.objects.all().order_by('last_name').values_list('employee_number', flat=True))
    position = id_list.index(unique_id)
    if 'next' in request.POST and position < id_list.index(max(id_list) - 1):
        position = position + 1
    elif 'previous' in request.POST and 0 < position:
        position = position - 1

    employee = Employees.objects.get(employee_number=id_list[position]).id

    return employee

```
