编辑:这是任何有兴趣的人的解决方案。我将Events模型中的 unicode 方法更改为以下
class Event(models.Model):
....
def __unicode__(self):
return '%s %s (%s)' % ((", ".join([str(item)for item in self.branches.all()])) , self.title, self.updated.strftime('%Y-%m-%d'))
(django V1.3,python 2.7)
标题令人困惑,我会尽力明确这一点。我有三个模型,分支,事件和更新:
class Branch(models.Model):
branch = models.CharField(max_length=20)
def __unicode__(self):
return self.branch
class Event(models.Model):
title = models.CharField(max_length=50)
branches = models.ManyToManyField(Branch)
updated = models.DateTimeField(auto_now=True)
def get_branches(self):
return ", ".join([str(p) for p in self.branches.all()])
def __unicode__(self):
return '%s (%s)' % (self.get_branches, self.title, self.updated.strftime('%Y-%m-%d'))
class Update(models.Model):
title = models.CharField(blank=False, max_length=45)
body = models.TextField(blank=False)
related_event = models.ManyToManyField(Event, blank=True)
def __unicode__(self):
return self.title
通过管理界面添加更新时,我希望 related_event 字段显示标题,分支并事件模型的更新字段,以便为用户更轻松地选择正确的 related_event (而不仅仅是一长串标题)。
添加更新时,我希望如何在下拉或水平 related_event 管理字段中显示此示例: ThisIsATitle Branch1,Branch2(yyyy-mm-dd)
我在 Event 模型中有这个简单的函数,它获取 Event 的所有分支并将它们连接成一个我成功使用的字符串在事件管理页面的list_display中:
def get_branches(self):
return ", ".join([str(p) for p in self.branches.all()])
活动管理员:
...
list_display = ('title','get_branches', 'updated')
...
我以为我可以像这样使用这个功能来实现我的目标:
def __unicode__(self):
return '%s (%s)' % (self.get_branches, self.title, self.updated.strftime('%Y-%m-%d'))
但它会抛出最大递归深度错误(仅适用于 title 和已更新。
/ admin / myapp / update / 8 /
中的TemplateSyntaxError渲染时捕获RuntimeError:调用Python对象时超出了最大递归深度
请求方法:GET
请求网址:http://example.com/admin/myapp/update/8/
Django版本:1.3 beta 1 SVN-15248
异常类型:TemplateSyntaxError
例外值:
渲染时捕获RuntimeError:调用Python对象时超出了最大递归深度
异常位置: unicode ,第168行中的/usr/share/django-apps/scpl/measures/models.py
Python可执行文件:/ usr / bin / python
环境:
模板错误:
在模板/usr/local/lib/python2.7/dist-packages/django/contrib/admin/templates/admin/includes/fieldset.html中,第19行出错
渲染时捕获RuntimeError:调用Python对象时超出了最大递归深度
追溯:
File" /usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py"在get_response
文件" /usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py"在包装器中
File" /usr/local/lib/python2.7/dist-packages/django/utils/decorators.py"在_wrapped_view中
File" /usr/local/lib/python2.7/dist-packages/django/views/decorators/cache.py"在_wrapped_view_func
中File" /usr/local/lib/python2.7/dist-packages/django/contrib/admin/sites.py"在内心
File" /usr/local/lib/python2.7/dist-packages/django/utils/decorators.py"在_wrapper中
File" /usr/local/lib/python2.7/dist-packages/django/utils/decorators.py"在_wrapped_view中
File" /usr/local/lib/python2.7/dist-packages/django/utils/decorators.py"在bound_func
中File" /usr/local/lib/python2.7/dist-packages/django/db/transaction.py"在内心
文件" /usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py"在change_view
文件" /usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py"在render_change_form
中文件" /usr/local/lib/python2.7/dist-packages/django/shortcuts/ init .py"在render_to_response
中File" /usr/local/lib/python2.7/dist-packages/django/template/loader.py"在render_to_string
中...
File" /usr/local/lib/python2.7/dist-packages/django/template/debug.py"在render_node
中File" /usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py"在渲染
File" /usr/local/lib/python2.7/dist-packages/django/template/base.py"在渲染
File" /usr/local/lib/python2.7/dist-packages/django/template/debug.py"在render_node
中File" /usr/local/lib/python2.7/dist-packages/django/template/debug.py"在渲染
File" /usr/local/lib/python2.7/dist-packages/django/utils/encoding.py"在force_unicode
File" /usr/local/lib/python2.7/dist-packages/django/forms/forms.py"在 unicode
File" /usr/local/lib/python2.7/dist-packages/django/forms/forms.py"在as_widget中
File" /usr/local/lib/python2.7/dist-packages/django/contrib/admin/widgets.py"在渲染
File" /usr/local/lib/python2.7/dist-packages/django/contrib/admin/widgets.py"在渲染
File" /usr/local/lib/python2.7/dist-packages/django/forms/widgets.py"在渲染
File" /usr/local/lib/python2.7/dist-packages/django/forms/widgets.py"在render_options中
File" /usr/local/lib/python2.7/dist-packages/django/forms/models.py"在 iter
File" /usr/local/lib/python2.7/dist-packages/django/forms/models.py"在选择
File" /usr/local/lib/python2.7/dist-packages/django/forms/models.py"在label_from_instance
中File" /usr/local/lib/python2.7/dist-packages/django/utils/encoding.py"在smart_unicode中
File" /usr/local/lib/python2.7/dist-packages/django/utils/encoding.py"在force_unicode
...
异常类型:/ admin / measures / update / 8 /
中的TemplateSyntaxError异常值:渲染时捕获RuntimeError:调用Python对象时超出了最大递归深度
答案 0 :(得分:1)
你在事件的__unicode__
方法中输了一个错字。
你正在做self.get_branches
,但你没有打电话给它(如self.get_branches()
)。
这将尝试打印类似“对象方法......”的内容,这可能会再次调用__unicode__
,从而导致循环。