Django分页器在最后一页没有响应 [英] Django paginator not responsive on last page

查看:174
本文介绍了Django分页器在最后一页没有响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在大型QuerySet上进行迭代似乎在Django 2.0中似乎不再是可行的选择。
我尝试使用Django自己的Paginator加快速度。

Iterating over a large QuerySet doesn't seem to be a viable option in Django 2.0 anymore. I've tried to speed it up using Django's own Paginator.

def read_from_db_4():
    paginator = Paginator(DataSet.objects.filter(status_id=1).order_by('id'), 1000)
    l = []
    print("{} iterations!".format(paginator.num_pages))
    for page in range(1, paginator.num_pages+1):
        l = l + list(paginator.page(page).object_list)
        print("{}, next page...".format(page))
    return l

这个小功能相当快但会停在最后一页。

This little function is reasonably quick but will stop on the very last page.

我也无法到达此页面的长度:

I can also not get to the length of this page:

len(paginator.page(LASTPAGE).object_list)

这将永远挂起。
我可以得到以前所有其他页面的长度。

this will just hang forever. I can get the length of all other pages previously.

这种奇怪行为的原因是什么?

What's the reason for this odd behaviour?

推荐答案

好的,我发现问题出在Python MySQL驱动程序上。

Okay, I've found the problem was the Python MySQL driver.

我没有使用推荐Django mysqlclient
我使用的是 Oracle / MySQL 创建的版本。似乎存在一个错误,该错误会导致迭代器在 for 循环中的QuerySet的最后一个元素上卡住,并在某些情况下陷入无限循环中。

I was not using the Django recommended mysqlclient. I was using the one created by Oracle/MySQL. There seems to be a bug that causes an iterator to get "stuck" on the last element of the QuerySet in a for loop and be trapped in an endless loop in certain circumstances.

我发现是让迭代器正常运行并在到达QuerySet中的最后一个元素时退出循环,例如,使用 .iterator()方法。但是,这对于我的用例来说不是一个选择。

I've found was of getting the iterator to behave normally and quit looping when it reached the last element in the QuerySet, by, for example, using the .iterator() method on it. However, that wasn't an option for the use case I had.

安装 mysqlclient 为我解决了这个问题。请注意,在使用Mac时,您还需要遵循关于macOS上MySQL Connector / C的错误的注意事项部分。

Installing mysqlclient solved the issue for me. Please note that when you are using a Mac, you will need to also follow the "Note about bug of MySQL Connector/C on macOS" section.

您可以找到 / usr / local / mysql / bin 文件夹中的 mysql_config 。最初,该指南使您搜索该文件,花了我很多时间才发现它,这让我有点不满意,因为搜索 mysql_config会为您提供数十个有关在何处找到 my.cnf

You can find the mysql_config in the folder /usr/local/mysql/bin. I was a bit put off initially by the fact that the guide makes you search for the file and it took me quite some time to find it, seeing that a search for "mysql_config" gives you dozens of results as to where to find the my.cnf.

这篇关于Django分页器在最后一页没有响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆