【问题标题】:How to display a ManyToMany field within the admin field of another ManyToMany field如何在另一个多对多字段的管理字段中显示多对多字段
【发布时间】: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)

标题令人困惑,我会尽力说明这一点。我有三个模型,BranchEventUpdate

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 字段显示 titlebranches 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'))

但它会引发最大递归深度错误(仅适用于 titleupdated


/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”

  1. response = callback(request, *callback_args, **callback_kwargs)

包装器中的文件“/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py”

  1. 返回 self.admin_site.admin_view(view)(*args, **kwargs)

_wrapped_view 中的文件“/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py”

  1. response = view_func(request, *args, **kwargs)

_wrapped_view_func 中的文件“/usr/local/lib/python2.7/dist-packages/django/views/decorators/cache.py”

  1. response = view_func(request, *args, **kwargs)

内部文件“/usr/local/lib/python2.7/dist-packages/django/contrib/admin/sites.py”

  1. 返回视图(请求,*args,**kwargs)

_wrapper 中的文件“/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py”

  1. return bound_func(*args, **kwargs)

_wrapped_view 中的文件“/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py”

  1. response = view_func(request, *args, **kwargs)

bound_func 中的文件“/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py”

  1. 返回函数(self, *args2, **kwargs2)

内部文件“/usr/local/lib/python2.7/dist-packages/django/db/transaction.py”

  1. res = func(*args, **kwargs)

change_view 中的文件“/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py”

  1. 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”

  1. ], 上下文, context_instance=context_instance)

render_to_response 中的文件“/usr/local/lib/python2.7/dist-packages/django/shortcuts/init.py”

  1. 返回 HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)

render_to_string 中的文件“/usr/local/lib/python2.7/dist-packages/django/template/loader.py”

  1. return t.render(context_instance)

...

  1. bits.append(self.render_node(node, context))

render_node 中的文件“/usr/local/lib/python2.7/dist-packages/django/template/debug.py”

  1. result = node.render(context)

渲染中的文件“/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py”

  1. 返回 self.nodelist_false.render(context)

渲染中的文件“/usr/local/lib/python2.7/dist-packages/django/template/base.py”

  1. bits.append(self.render_node(node, context))

render_node 中的文件“/usr/local/lib/python2.7/dist-packages/django/template/debug.py”

  1. result = node.render(context)

渲染中的文件“/usr/local/lib/python2.7/dist-packages/django/template/debug.py”

  1. 输出 = force_unicode(输出)

force_unicode 中的文件“/usr/local/lib/python2.7/dist-packages/django/utils/encoding.py”

  1. s = unicode(s)

unicode中的文件“/usr/local/lib/python2.7/dist-packages/django/forms/forms.py”

  1. 返回 self.as_widget()

as_widget 中的文件“/usr/local/lib/python2.7/dist-packages/django/forms/forms.py”

  1. return widget.render(name, self.value(), attrs=attrs)

渲染中的文件“/usr/local/lib/python2.7/dist-packages/django/contrib/admin/widgets.py”

  1. 输出 = [self.widget.render(name, value, *args, **kwargs)]

渲染中的文件“/usr/local/lib/python2.7/dist-packages/django/contrib/admin/widgets.py”

  1. output = [super(FilteredSelectMultiple, self).render(name, value, attrs, choice)]

渲染中的文件“/usr/local/lib/python2.7/dist-packages/django/forms/widgets.py”

  1. options = self.render_options(choices, value)

render_options 中的文件“/usr/local/lib/python2.7/dist-packages/django/forms/widgets.py”

  1. 对于 option_value,链中的 option_label(self.choices,choices):

iter中的文件“/usr/local/lib/python2.7/dist-packages/django/forms/models.py”

  1. yield self.choice(obj)

选择文件“/usr/local/lib/python2.7/dist-packages/django/forms/models.py”

  1. 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”

  1. 返回 smart_unicode(obj)

smart_unicode 中的文件“/usr/local/lib/python2.7/dist-packages/django/utils/encoding.py”

  1. return force_unicode(s, encoding, strings_only, errors)

force_unicode 中的文件“/usr/local/lib/python2.7/dist-packages/django/utils/encoding.py”

  1. s = unicode(s)

...

异常类型:/admin/measures/update/8/ 处的 TemplateSyntaxError

异常值:渲染时捕获 RuntimeError:调用 Python 对象时超出最大递归深度

【问题讨论】:

  • 这无关紧要,但我建议从不受支持且不安全的 1.3 升级到 1.10.1。
  • 不幸的是,此时升级不是一个选项。我们正在构建一个新的 Web 服务器,以期待下一个 LTS 版本
  • Event admin 中有适当的内联吗?
  • 我不需要从更新管理界面更改任何其他模型,所以我不需要内联。
  • 你能放一个递归错误堆栈跟踪的sn-p吗?

标签: django


【解决方案1】:

您的 Event 的 __unicode__ 方法中有错字。

你正在调用self.get_branches,但你没有调用它(比如self.get_branches())。

这将尝试打印类似“对象方法...”之类的内容,可能会尝试再次调用__unicode__,从而导致循环。

【讨论】:

  • 谢谢,我最终将连接添加到 unicode 方法中:def __unicode__(self):return '%s %s (%s)' % ((", ".join([str(item)for item in self.branches.all()])) , self.title, self.updated.strftime('%Y-%m-%d'))
  • 请接受这个答案,因为这解决了您的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-20
  • 1970-01-01
  • 2011-12-24
  • 2014-10-20
  • 2019-01-26
  • 2022-11-08
  • 2011-06-26
相关资源
最近更新 更多