Django 最大递归深度超出
我正在用Django做一个小的网页项目,里面有一个模型叫做Image
,这个模型里有一个ImageField
。当我试图通过管理界面上传图片时,遇到了一个问题(个人信息已删除):
RuntimeError at /admin/main/image/add/
maximum recursion depth exceeded
Request Method: POST
Request URL: http://x.x.x.x:8080/blog/admin/main/image/add/
Django Version: 1.2.1
Exception Type: RuntimeError
Exception Value:
maximum recursion depth exceeded
Exception Location: /extra/django/blog/main/models.py in __unicode__, line 26
Python Executable: /usr/bin/python
Python Version: 2.4.3
Python Path: ['/extra/django', '/usr/lib/python2.4/site-packages/setuptools-0.6c11-py2.4.egg', '/usr/lib/python2.4/site-packages/MySQL_python-1.2.3-py2.4-linux-i686.egg', '/usr/lib/python24.zip', '/usr/lib/python2.4', '/usr/lib/python2.4/plat-linux2', '/usr/lib/python2.4/lib-tk', '/usr/lib/python2.4/lib-dynload', '/usr/lib/python2.4/site-packages', '/usr/lib/python2.4/site-packages/Numeric', '/usr/lib/python2.4/site-packages/PIL', '/usr/lib/python2.4/site-packages/gtk-2.0']
Server time: Tue, 17 Aug 2010 13:30:20 -0400
这是我models.py
文件的一部分:
class Image(models.Model):
image = models.ImageField(upload_to='uploads/blog_images')
caption = models.CharField(max_length=300)
post = models.ForeignKey('Post')
thumbWidth = models.IntegerField(blank=True,null=True)
thumbHeight = models.IntegerField(blank=True,null=True)
def printTag(self, newClass=''):
str = '<img '
if newClass is not '':
str = str + 'class="%s" ' %newClass
if self.thumbWidth is not None and self.thumbHeight is not None:
str += 'width="%i" height="%i" ' %(self.thumbWidth,self.thumbHeight)
str = str + 'src="%s" ' %self.image
str = str + '>%s</img>' %self.caption
return str
def __unicode__(self):
return self.printTag(self)
第26行是unicode里面唯一的一行。我有一个额外的函数(printTag),这样我可以选择是否打印带有“class”属性的HTML标签,默认是不带属性的。为什么在我上传图片的时候会出现递归的问题呢?
1 个回答
4
你需要用 return self.printTag()
这个写法,而不是 return self.printTag(self)
这个。