开发者

django pagination not working?

It seems that pagination in Django 1.2.3 is not working. I'm simply trying to run a query, split the results into pages of 200 objects each, and do something with the results from each page. I probably don't need to use the Paginator for this but I thought it was nice and convenient. But it seems to be giving random results - i.e. some of the objects appear on multiple pages and some do not appear on any pages. I guess that behind the scenes perhaps it is running multiple database queries and because I don't have an order_by statement the results are coming back in a different order each time? Well I'm not sure why my database (Postgres) would be giving items back in a different order each time (incidentally the data in 开发者_高级运维the database is not changing). If I add an order_by to the query it seems to fix the problem. If I run this on a test database built using pg_dump/pg_restore I don't seem to have the problem (I guess the test database is somehow returning data in a consistent order). Incidentally there is only one "EQIX" row in the database.

secs = Security.objects.filter(current=True)

print 'test1'
p = Paginator(secs, 200)
for pagenumber in p.page_range:
    page = p.page(pagenumber)
    for i, sec in enumerate(page.object_list):
        if sec.ibsymbol == 'EQIX':
            print 'EQIX'

print 'test2'
for sec in secs:
    if sec.ibsymbol == 'EQIX':
        print 'EQIX'


trial run #1 output
test1
test2
EQIX

trial run #2 output
test1
EQIX
EQIX
EQIX
test2
EQIX
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜