Django 运行时错误: 最大递归深度超过限制

3

我是Django的新手。我在Mac上使用easy_install安装了Django和PyDev Django插件。我按照标准程序创建了一个新的PyDev Django项目。当我尝试以PyDev: Django的形式运行该项目时,出现以下错误。

Validating models...

RuntimeError: maximum recursion depth exceeded

我也尝试在manage.py中添加以下行,但没有用。
sys.setrecursionlimit(2000)

这里是部分堆栈跟踪信息。

Unhandled exception in thread started by <bound method Command.inner_run of    <django.contrib.staticfiles.management.commands.runserver.Command object at 0x10e2*****>>
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/Django-1.5-py2.7.egg/django/core/management/commands/runserver.py", line 92, in inner_run
self.validate(display_num_errors=True)
File "/Library/Python/2.7/site-packages/Django-1.5-py2.7.egg/django/core/management/base.py", line 280, in validate
num_errors = get_validation_errors(s, app)
File "/Library/Python/2.7/site-packages/Django-1.5-py2.7.egg/django/core/management/validation.py", line 35, in get_validation_errors
for (app_name, error) in get_app_errors().items():
File "/Library/Python/2.7/site-packages/Django-1.5-py2.7.egg/django/db/models/loading.py", line 166, in get_app_errors
self._populate()
File "/Library/Python/2.7/site-packages/Django-1.5-py2.7.egg/django/db/models/loading.py", line 72, in _populate
self.load_app(app_name, True)
File "/Library/Python/2.7/site-packages/Django-1.5-py2.7.egg/django/db/models/loading.py", line 96, in load_app
models = import_module('.models', app_name)
File "/Library/Python/2.7/site-packages/Django-1.5-py2.7.egg/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/Library/Python/2.7/site-packages/Django-1.5-py2.7.egg/django/contrib/auth/models.py", line 370, in <module>
class AbstractUser(AbstractBaseUser, PermissionsMixin):
File "/Library/Python/2.7/site-packages/Django-1.5-py2.7.egg/django/db/models/base.py", line 213, in __new__
new_class.add_to_class(field.name, copy.deepcopy(field))
File "/Library/Python/2.7/site-packages/Django-1.5-py2.7.egg/django/db/models/base.py", line 265, in add_to_class
value.contribute_to_class(cls, name)
File "/Library/Python/2.7/site-packages/Django-1.5-py2.7.egg/django/db/models/fields/__init__.py", line 257, in contribute_to_class
cls._meta.add_field(self)
File "/Library/Python/2.7/site-packages/Django-1.5-py2.7.egg/django/db/models/options.py", line 179, in add_field
self.local_fields.insert(bisect(self.local_fields, field), field)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/functools.py", line 56, in <lambda>
'__lt__': [('__gt__', lambda self, other: other < self),
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/functools.py", line 56, in <lambda>
'__lt__': [('__gt__', lambda self, other: other < self),
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/functools.py", line 56, in <lambda>
'__lt__': [('__gt__', lambda self, other: other < self),
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/functools.py", line 56, in <lambda>
'__lt__': [('__gt__', lambda self, other: other < self),

我做错了什么?

你确定你的模型没有相互引用或其他奇怪的东西吗? - GordonsBeard
这只是一个空项目,具有与 Django 的官方网站上规定的相同结构和内容。 - Ashwin
请问您能否提供堆栈跟踪信息? - andrefsp
@Ashwin,看起来是模型出了问题。我猜你正在使用Django自定义用户模型。对吗? 溯源并没有太多的信息,但我建议你查看一下授权模型导入的周围。 你可能引用了某些自定义的授权模型,并在其他地方再次导入它。 - andrefsp
Django Python中的manage.py runserver命令出现RuntimeError: maximum recursion depth exceeded错误。 - user9876
显示剩余2条评论
3个回答

23

问题出现在functools.py文件中,该文件来自Python。

要解决这个问题,请替换此处(位于python\Lib\fuctools.py的第56行左右):

    convert = {
    '__lt__': [('__gt__', lambda self, other: other < self),
               ('__le__', lambda self, other: not other < self),
               ('__ge__', lambda self, other: not self < other)],
    '__le__': [('__ge__', lambda self, other: other <= self),
               ('__lt__', lambda self, other: not other <= self),
               ('__gt__', lambda self, other: not self <= other)],
    '__gt__': [('__lt__', lambda self, other: other > self),
               ('__ge__', lambda self, other: not other > self),
               ('__le__', lambda self, other: not self > other)],
    '__ge__': [('__le__', lambda self, other: other >= self),
               ('__gt__', lambda self, other: not other >= self),
               ('__lt__', lambda self, other: not self >= other)]
}

到那:

    convert = {
    '__lt__': [('__gt__', lambda self, other: not (self < other or self == other)),
               ('__le__', lambda self, other: self < other or self == other),
               ('__ge__', lambda self, other: not self < other)],
    '__le__': [('__ge__', lambda self, other: not self <= other or self == other),
               ('__lt__', lambda self, other: self <= other and not self == other),
               ('__gt__', lambda self, other: not self <= other)],
    '__gt__': [('__lt__', lambda self, other: not (self > other or self == other)),
               ('__ge__', lambda self, other: self > other or self == other),
               ('__le__', lambda self, other: not self > other)],
    '__ge__': [('__le__', lambda self, other: (not self >= other) or self == other),
               ('__gt__', lambda self, other: self >= other and not self == other),
               ('__lt__', lambda self, other: not self >= other)]
}

阅读这篇文章:http://regebro.wordpress.com/2010/12/13/python-implementing-rich-comparison-the-correct-way/


这个解决了在Windows 10上的问题,编辑文件C:\Python27\Lib\functools.py。 - Daniel Neel
这个答案非常有用并解决了我的问题,但你是如何找到错误原因的呢? - Harun ERGUL

0

我在使用 pyenv virtualenvs 和 Python 2.7.1 时遇到过几次这个问题。我不想编辑核心文件,所以我升级到了 2.7.5,并且它完美地解决了这个问题。希望这对你们中的一些人来说是一个选项。


0

我卸载了PyDev、Django和Python,然后重新安装它们,现在它可以正常工作了。谢谢!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接