django-tables2:添加在模型中找不到的新自定义列 [英] django-tables2: Adding a new custom column not found in model

查看:97
本文介绍了django-tables2:添加在模型中找不到的新自定义列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下表:

class TaskTable(tables.Table):

    def render_foo(self):
        raise Exception()

    class Meta:
        model = Task
        fields = ['foo']

适用于以下型号:

class Task(models.Model):
    priority = models.PositiveIntegerField()
    title = models.CharField(max_length=1024)
    content = models.TextField(null=True, blank=True)

根据文档,如果将 render_foo 视为空值,则不会执行.

According to the docs, render_foo won't be executed if it is considered an empty value.

我的要求是在模型中找不到 foo .如何实现列 foo ?

My requirement is that foo is not found in the Model. How can I implement the column foo?

推荐答案

您必须将 empty_values =()关键字参数传递给该列,以引用

You have to pass an empty_values=() keyword argument to the column, to quote the docs:

仅当确定单元格的值不是空值时,才调用

render方法.当值位于 Column.empty_values 中时,将呈现默认值(跳过Column.render和 Table.render_FOO ).

render methods are only called if the value for a cell is determined to be not an empty value. When a value is inColumn.empty_values, a default value is rendered instead (both Column.render and Table.render_FOO are skipped).

在您的情况下,不存在的列的值"为,因此,如果该值在 empty_values 中,则为默认值(-默认呈现).

In your case, the 'value' for your non-existing column is None, so if that value is in empty_values, the default value (- by default is rendered).

所以在代码中,它可能看起来像这样:

So in code, it could look like this:

class TaskTable(tables.Table):
    foo = tables.Column(empty_values=())

    def render_foo(self):
        raise Exception()

    class Meta:
        model = Task

相关文档

这篇关于django-tables2:添加在模型中找不到的新自定义列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆