当前位置: 首页 > 知识库问答 >
问题:

如何在另一个多人字段的管理字段中显示多人字段

逄嘉熙
2023-03-14

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

当通过管理界面添加更新时,我希望相关的_事件字段显示事件模型的标题、分支和更新字段,以便于用户选择正确的相关_事件(而不仅仅是一长串标题)。

添加更新时,我希望如何在下拉或水平相关的_事件管理字段中显示此信息的示例:ThisIsATitle Branch1,Branch2(yyyy-mm-dd)

我在事件模型中有一个简单的函数,它获取事件的所有分支,并将它们连接到一个字符串中,我成功地在事件管理页面的列表显示中使用了该字符串:

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'))

但它抛出了一个最大递归深度错误(仅适用于标题和更新)。

TemplateSyntaxError位于/admin/myapp/update/8/

呈现时捕获到运行时错误:调用Python对象时超过最大递归深度

请求方法:获取

请求URL:http://example.com/admin/myapp/update/8/

Django版本:1.3 beta 1 SVN-15248

异常类型:TemplateSyntaxError

异常值:

呈现时捕获到运行时错误:调用Python对象时超过最大递归深度

异常位置:/usr/share/django apps/scpl/measures/models。unicode中的py,第168行

Python可执行文件:/usr/bin/Python

环境:

模板错误:

在模板 /usr/local/lib/python2.7/dist-packages/django/contrib/admin/templates/admin/includes/fieldset.html,错误在第19行

呈现时捕获到运行时错误:调用Python对象时超过最大递归深度

回溯:

get_响应中的文件“/usr/local/lib/python2.7/dist packages/django/core/handlers/base.py”

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

在_wrapped_view中文件"/usr/本地/lib/python2.7/dist-包/django/utils/decorators.py"

在_wrapped_view_func中文件"/usr/本地/lib/python2.7/dist-包/django/视图/装饰器/cache.py"

在内部文件"/usr/本地/lib/python2.7/dist-包/django/contrib/admin/sites.py"

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

在_wrapped_view中文件"/usr/本地/lib/python2.7/dist-包/django/utils/decorators.py"

在bound_func中文件"/usr/本地/lib/python2.7/dist-包/django/utils/decorators.py"

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

在change_view中文件"/usr/local/lib/python2.7/dist-包/django/contrib/admin/options.py"

文件“/usr/local/lib/python2.7/dist packages/django/contrib/admin/options.py”,格式为render\u change\u

在render_to_response中文件"/usr/本地/lib/python2.7/dist-包/django/快捷方式/init.py"

render_to_字符串中的文件“/usr/local/lib/python2.7/dist packages/django/template/loader.py”

...

render_节点中的文件“/usr/local/lib/python2.7/dist packages/django/template/debug.py”

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

在渲染中文件"/usr/本地/lib/python2.7/dist-包/django/模板/base.py"

render_节点中的文件“/usr/local/lib/python2.7/dist packages/django/template/debug.py”

在渲染中文件"/usr/本地/lib/python2.7/dist-包/django/模板/debug.py"

强制unicode格式的文件“/usr/local/lib/python2.7/dist packages/django/utils/encoding.py”

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

as_小部件中的文件“/usr/local/lib/python2.7/dist packages/django/forms/forms.py”

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

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

在渲染中文件"/usr/本地/lib/python2.7/dist-包/django/窗体/widgets.py"

render_选项中的文件“/usr/local/lib/python2.7/dist packages/django/forms/widgets.py”

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

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

文件“/usr/local/lib/python2.7/dist packages/django/forms/models.py”,位于label\u from\u实例中

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

强制unicode格式的文件“/usr/local/lib/python2.7/dist packages/django/utils/encoding.py”

...

异常类型:TemplateSyntaxError位于/admin/measures/update/8/

异常值:渲染时捕获运行时错误:调用Python对象时超过了最大递归深度

共有1个答案

充培
2023-03-14

事件的__unicode__方法中有一个错别字。

您正在做self.get_branches,但您没有调用它(像self.get_branches())。

这将尝试打印类似“对象方法…”的内容它可能会再次尝试调用\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。

 类似资料:
  • 假设我已将以下对象序列化为json字符串: 现在我想反序列化它,但是我想把名称分成两个字段,和。我该怎么做呢? 我希望最终的结果是类似于: 这对Gson有可能吗?

  • 问题内容: 我正在使用’Lucene.Net’库,并且有以下问题。 不是创建两个单独的QueryParser对象并使用它们来获得两个Hits对象,而是可以使用一个QueryParser对象在两个字段上进行搜索,这样我只有一个Hits对象,它可以为我提供每个文档的总体得分? 问题答案: 有3种方法可以做到这一点。 第一种方法是手动构造查询,这是QueryParser内部的工作。这是执行此操作最强大的

  • 索引模式中的字段会以列表的方式显示,点击列标题可以按列对表格排序,点击最右边列的 控制 按钮可以编辑指定字段的属性。您可以从 格式 下拉菜单中手动设置字段的格式。具体格式选项依赖于字段类型。 您也可以在 流行度 文本框中设置您需要的该字段的流行度值。点击 更新字段 按钮确认更改,或者点击 取消 返回字段列表。 Kibana 有以下字段类型的格式化: 字符串字段格式化 字符串字段支持 String

  • 问题内容: 如何查询或过滤一个字段不等于另一个字段?即,其中document1.city1.name不等于document1.city2.name。 这个的一些版本? http://www.elasticsearch.org/guide/zh- CN/elasticsearch/reference/current/search-request-script- fields.html 问题答案: 是

  • 我有2 JTextField和2 JPasswordField组件在我的摇摆java框架看起来如下:当ShowAll细节按钮被点击时,所有输入的细节显示在Jtext Area如下所示: 下面是相同的代码: 现在,我不想在加载主框架时显示这4个组件,而是想单击按钮在弹出窗口中显示这4个组件,如下所示: 同样在点击显示所有细节按钮应该像以前一样工作。(即,它应该用弹出窗口中输入的值填充JextArea

  • 问题内容: 我有以下型号: 在管理界面中,当创建新演示时,在用户从下拉列表中选择类别之后,我想限制“项目”下拉列表中的选择数量。如果用户选择其他类别,则项目选择应相应更新。我想在客户端上限制项目选择,甚至在服务器上进行表单验证之前。这是为了提高可用性,因为项目列表可能超过1000个,能够按类别缩小范围将有助于使其更易于管理。 是这样做的“捷径”还是自定义JavaScript是这里的唯一选择? 问题