You can make two requests: one for users in front of current users and one for users immediately after:
id = current_user.pk
points = current_user.profile.points
before = User.objects.filter(
Q(profile__points__gt=points) |
Q(profile__points=points, pk__lt=id)
).order_by('-profile__points')[:5]
after = User.objects.filter(
Q(profile__points__lt=points) |
Q(profile__points=points, pk__gt=id)
).order_by('profile__points')[:5]
These are the basics for two queries:
- All users with an account larger than the current user, or with the same account, but with a lower one
pk. - All users with an account lower than the current user or with the same account, but with a larger one
pk.
. , pk . , ( ), :
before = User.objects.filter(
profile__points__gt=points,
).order_by('-profile__points')[:5]
after = User.objects.filter(
profile__points__lte=points,
).exclude(pk=id).order_by('profile__points')[:5]
, , , :
id = current_user.pk
points = current_user.profile.points
index = User.objects.filter(
Q(profile__points__gt=points) |
Q(profile__points=points, pk__lt=id)
).count()
, , :
User.objects.all().order_by('-profile__points', 'pk')[index - 5:index + 6]
, , , .