如何在另一个ManyToMany字段

时间:2016-09-23 18:04:09

标签: django

编辑:这是任何有兴趣的人的解决方案。我将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

  1. response = callback(request,* callback_args,** callback_kwargs)
  2. 文件" /usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py"在包装器中

    1. return self.admin_site.admin_view(view)(* args,** kwargs)
    2. File" /usr/local/lib/python2.7/dist-packages/django/utils/decorators.py"在_wrapped_view中

      1. response = view_func(request,* args,** kwargs)
      2. File" /usr/local/lib/python2.7/dist-packages/django/views/decorators/cache.py"在_wrapped_view_func

        1. response = view_func(request,* args,** kwargs)
        2. File" /usr/local/lib/python2.7/dist-packages/django/contrib/admin/sites.py"在内心

          1. 返回视图(request,* args,** kwargs)
          2. File" /usr/local/lib/python2.7/dist-packages/django/utils/decorators.py"在_wrapper中

            1. return bound_func(* args,** kwargs)
            2. File" /usr/local/lib/python2.7/dist-packages/django/utils/decorators.py"在_wrapped_view中

              1. response = view_func(request,* args,** kwargs)
              2. File" /usr/local/lib/python2.7/dist-packages/django/utils/decorators.py"在bound_func

                1. return func(self,* args2,** kwargs2)
                2. File" /usr/local/lib/python2.7/dist-packages/django/db/transaction.py"在内心

                  1. res = func(* args,** kwargs)
                  2. 文件" /usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py"在change_view

                    1. return self.render_change_form(request,context,change = True,obj = obj)
                    2. 文件" /usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py"在render_change_form

                      1. ],context,context_instance = context_instance)
                      2. 文件" /usr/local/lib/python2.7/dist-packages/django/shortcuts/ init .py"在render_to_response

                        1. 返回HttpResponse(loader.render_to_string(* args,** kwargs),** httpresponse_kwargs)
                        2. File" /usr/local/lib/python2.7/dist-packages/django/template/loader.py"在render_to_string

                          1. return t.render(context_instance)
                          2. ...

                            1. bits.append(self.render_node(node,context))
                            2. File" /usr/local/lib/python2.7/dist-packages/django/template/debug.py"在render_node

                              1. result = node.render(context)
                              2. File" /usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py"在渲染

                                1. return self.nodelist_false.render(context)
                                2. File" /usr/local/lib/python2.7/dist-packages/django/template/base.py"在渲染

                                  1. bits.append(self.render_node(node,context))
                                  2. File" /usr/local/lib/python2.7/dist-packages/django/template/debug.py"在render_node

                                    1. result = node.render(context)
                                    2. File" /usr/local/lib/python2.7/dist-packages/django/template/debug.py"在渲染

                                      1. output = force_unicode(输出)
                                      2. File" /usr/local/lib/python2.7/dist-packages/django/utils/encoding.py"在force_unicode

                                        1. s = unicode(s)
                                        2. File" /usr/local/lib/python2.7/dist-packages/django/forms/forms.py"在 unicode

                                          1. return self.as_widget()
                                          2. File" /usr/local/lib/python2.7/dist-packages/django/forms/forms.py"在as_widget中

                                            1. 返回widget.render(name,self.value(),attrs = attrs)
                                            2. File" /usr/local/lib/python2.7/dist-packages/django/contrib/admin/widgets.py"在渲染

                                              1. output = [self.widget.render(name,value,* args,** kwargs)]
                                              2. File" /usr/local/lib/python2.7/dist-packages/django/contrib/admin/widgets.py"在渲染

                                                1. output = [super(FilteredSelectMultiple,self).render(name,value,attrs,choices)]
                                                2. File" /usr/local/lib/python2.7/dist-packages/django/forms/widgets.py"在渲染

                                                  1. options = self.render_options(选项,值)
                                                  2. File" /usr/local/lib/python2.7/dist-packages/django/forms/widgets.py"在render_options中

                                                    1. for option_value,chain_label in chain(self.choices,choices):
                                                    2. File" /usr/local/lib/python2.7/dist-packages/django/forms/models.py"在 iter

                                                      1. yield self.choice(obj)
                                                      2. File" /usr/local/lib/python2.7/dist-packages/django/forms/models.py"在选择

                                                        1. return(self.field.prepare_value(obj),self.field.label_from_instance(obj))
                                                        2. File" /usr/local/lib/python2.7/dist-packages/django/forms/models.py"在label_from_instance

                                                          1. 返回smart_unicode(obj)
                                                          2. File" /usr/local/lib/python2.7/dist-packages/django/utils/encoding.py"在smart_unicode中

                                                            1. return force_unicode(s,encoding,strings_only,errors)
                                                            2. File" /usr/local/lib/python2.7/dist-packages/django/utils/encoding.py"在force_unicode

                                                              1. s = unicode(s)
                                                              2. ...

                                                                异常类型:/ admin / measures / update / 8 /

                                                                中的TemplateSyntaxError

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

1 个答案:

答案 0 :(得分:1)

你在事件的__unicode__方法中输了一个错字。

你正在做self.get_branches,但你没有打电话给它(如self.get_branches())。

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