1
As you have realised using just the created at date doesn’t give you deterministic sort, you should also sort by the id of the pictures. The previous picture is then one with a created date less than the current image, or with the same created date and the previous id.
Because this is a complicated query you need to use Q objects.
next = Picture.objects.filter(Q(created_at__lt=image.created_at) | Q(created_at=image.created_at, id__lt=image.id))
if next:
next = next.order_by("-created_at", "id")[0]
Source:stackexchange.com