【发布时间】:2017-02-01 16:21:17
【问题描述】:
编辑:这是任何有兴趣的人的修复。我将事件模型中的 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)
标题令人困惑,我会尽力说明这一点。我有三个模型,Branch、Event和Update:
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 字段显示 title、branches Event 模型的 更新 字段,以使用户更容易选择正确的 related_event(而不仅仅是一长串标题)。
添加 Update 时我希望它如何显示在下拉或水平 related_event 管理字段中的示例: ThisIsATitle Branch1,Branch2 (yyyy-mm-dd)
我在 Event 模型中有一个简单的函数,它获取一个 Event 的所有 branches 并将它们连接成一个我成功使用的字符串在 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 和 updated。
/admin/myapp/update/8/ 处的模板语法错误
渲染时捕获 RuntimeError:调用 Python 对象时超出最大递归深度
请求方法:GET
请求网址:http://example.com/admin/myapp/update/8/
Django 版本:1.3 beta 1 SVN-15248
异常类型:TemplateSyntaxError
异常值:
渲染时捕获 RuntimeError:调用 Python 对象时超出最大递归深度
异常位置:/usr/share/django-apps/scpl/measures/models.py in unicode,第 168 行
Python 可执行文件:/usr/bin/python
环境:
模板错误:
在模板/usr/local/lib/python2.7/dist-packages/django/contrib/admin/templates/admin/includes/fieldset.html,第19行错误
渲染时捕获 RuntimeError:调用 Python 对象时超出最大递归深度
追溯:
get_response 中的文件“/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py”
- response = callback(request, *callback_args, **callback_kwargs)
包装器中的文件“/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py”
- 返回 self.admin_site.admin_view(view)(*args, **kwargs)
_wrapped_view 中的文件“/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py”
- response = view_func(request, *args, **kwargs)
_wrapped_view_func 中的文件“/usr/local/lib/python2.7/dist-packages/django/views/decorators/cache.py”
- response = view_func(request, *args, **kwargs)
内部文件“/usr/local/lib/python2.7/dist-packages/django/contrib/admin/sites.py”
- 返回视图(请求,*args,**kwargs)
_wrapper 中的文件“/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py”
- return bound_func(*args, **kwargs)
_wrapped_view 中的文件“/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py”
- response = view_func(request, *args, **kwargs)
bound_func 中的文件“/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py”
- 返回函数(self, *args2, **kwargs2)
内部文件“/usr/local/lib/python2.7/dist-packages/django/db/transaction.py”
- res = func(*args, **kwargs)
change_view 中的文件“/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py”
- return self.render_change_form(request, context, change=True, obj=obj)
render_change_form 中的文件“/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py”
- ], 上下文, context_instance=context_instance)
render_to_response 中的文件“/usr/local/lib/python2.7/dist-packages/django/shortcuts/init.py”
- 返回 HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
render_to_string 中的文件“/usr/local/lib/python2.7/dist-packages/django/template/loader.py”
- return t.render(context_instance)
...
- bits.append(self.render_node(node, context))
render_node 中的文件“/usr/local/lib/python2.7/dist-packages/django/template/debug.py”
- result = node.render(context)
渲染中的文件“/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py”
- 返回 self.nodelist_false.render(context)
渲染中的文件“/usr/local/lib/python2.7/dist-packages/django/template/base.py”
- bits.append(self.render_node(node, context))
render_node 中的文件“/usr/local/lib/python2.7/dist-packages/django/template/debug.py”
- result = node.render(context)
渲染中的文件“/usr/local/lib/python2.7/dist-packages/django/template/debug.py”
- 输出 = force_unicode(输出)
force_unicode 中的文件“/usr/local/lib/python2.7/dist-packages/django/utils/encoding.py”
- s = unicode(s)
unicode中的文件“/usr/local/lib/python2.7/dist-packages/django/forms/forms.py”
- 返回 self.as_widget()
as_widget 中的文件“/usr/local/lib/python2.7/dist-packages/django/forms/forms.py”
- return widget.render(name, self.value(), attrs=attrs)
渲染中的文件“/usr/local/lib/python2.7/dist-packages/django/contrib/admin/widgets.py”
- 输出 = [self.widget.render(name, value, *args, **kwargs)]
渲染中的文件“/usr/local/lib/python2.7/dist-packages/django/contrib/admin/widgets.py”
- output = [super(FilteredSelectMultiple, self).render(name, value, attrs, choice)]
渲染中的文件“/usr/local/lib/python2.7/dist-packages/django/forms/widgets.py”
- options = self.render_options(choices, value)
render_options 中的文件“/usr/local/lib/python2.7/dist-packages/django/forms/widgets.py”
- 对于 option_value,链中的 option_label(self.choices,choices):
iter中的文件“/usr/local/lib/python2.7/dist-packages/django/forms/models.py”
- yield self.choice(obj)
选择文件“/usr/local/lib/python2.7/dist-packages/django/forms/models.py”
- return (self.field.prepare_value(obj), self.field.label_from_instance(obj))
label_from_instance 中的文件“/usr/local/lib/python2.7/dist-packages/django/forms/models.py”
- 返回 smart_unicode(obj)
smart_unicode 中的文件“/usr/local/lib/python2.7/dist-packages/django/utils/encoding.py”
- return force_unicode(s, encoding, strings_only, errors)
force_unicode 中的文件“/usr/local/lib/python2.7/dist-packages/django/utils/encoding.py”
- s = unicode(s)
...
异常类型:/admin/measures/update/8/ 处的 TemplateSyntaxError
异常值:渲染时捕获 RuntimeError:调用 Python 对象时超出最大递归深度
【问题讨论】:
-
这无关紧要,但我建议从不受支持且不安全的 1.3 升级到 1.10.1。
-
不幸的是,此时升级不是一个选项。我们正在构建一个新的 Web 服务器,以期待下一个 LTS 版本
-
Eventadmin 中有适当的内联吗? -
我不需要从更新管理界面更改任何其他模型,所以我不需要内联。
-
你能放一个递归错误堆栈跟踪的sn-p吗?
标签: django