Opened 8 years ago

Closed 8 years ago

#26160 closed Bug (invalid)

[offset:limit] doesn't do anything

Reported by: Ivan Maslov Owned by: nobody
Component: Database layer (models, ORM) Version: 1.8
Severity: Normal Keywords: offset, limit
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Env: postresql on remote server, django==1.8.8, psycopg2==2.6.1

Models:

class ProjectUser(AbstractBaseUser, PermissionsMixin):
    # ...


class Transaction(models.Model):
    # ...
    sent_time = models.DateTimeField(default=timezone.now)
    user = models.ForeignKey(ProjectUser, null=True, default=None)

I trying to check it by shell (with sql dump):
u = models.ProjectUser.objects.get(id=1) # ok - this is my first project user
I see "SELECT BLABLABLA WHERE BLABLA."id" = 1
After this:
tr = u.transaction_set.order_by('-sent_time')
tr.count()
I see "SELECT COUNT(*) BLABLABLA FROM BLABLABLA = 1"
Out: 14 # 14 rows in the database

tr[0:10]
I see "SELECT BLABLABLA DESC LIMIT 10" and ten transactions into a queryset

And finally do:
tr[10:10]

And nothing.
Only [] in shell.

But if i will use a Paginator, it works correctly

list(paginator.page(2))
[<Transaction: Transaction object>,
 <Transaction: Transaction object>,
 <Transaction: Transaction object>,
 <Transaction: Transaction object>]

Change History (1)

comment:1 by Shai Berger, 8 years ago

Resolution: invalid
Status: newclosed

It isn't exactly offset:limit the way you think of it -- it uses Python's slicing semantics.

You need [10:20] to get what you want.

In the future, please ask first in one of the users forums -- the django-users google-group or the #django IRC channel.

Note: See TracTickets for help on using tickets.
Back to Top