How to get the next object by id in DetailView

You can also use subqueries to retrieve previous and next ids as annotations to avoid two extra queries

queryset = ObjetArchi.objects.annotate(
     previous_id=ObjetArchi.objects.filter(
         id__lt=OuterRef("id"),
     ).order_by("-id").values("id")[:1],
     next_id=ObjetArchi.objects.filter(
         id__gt=OuterRef("id"),
     ).order_by("id").values("id")[:1]
).get(id=pk)
4 Likes