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)
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?
I was already thinking about implementing kind of alphabetical_idfield 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…
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