django - 如何将计算列添加到 django 模型的列表页面?

标签 django templates view

我有一个模型,

class Thingie(models.Model):
    thingie_name = models.CharField(max_length=16, unique=True)
    interesting_property = models.BogusField(...) 

...并在 admin.py 中:

class ThingieAdmin(adimn.ModelAdmin):
    list_display = ('thingie_name', 'interesting_property')

当我查看管理页面时,我看到如下内容:

Select Thingie to change

Thingie name    | Interesting property
monkey_nuggets  | you really don't want to know
yodeling_yoda   | this is going downhill fast

我想要的是这里有第三个字段,生成的 URL:

Thingie name    | ... | For more info...
monkey_nuggets  | ... | http://foo.com/thingie/monkey_nuggets
yodeling_yoda   | ... | http://foo.com/thingie/yodeling_yoda

请注意,我不想将人们发送到此属性的编辑页面。我只需要一种方法来影响 View 的渲染,以便我可以插入不受数据库支持的自定义列,它只是根据其他值之一进行计算。

最佳答案

您可以在模型管理中定义一个方法来执行此操作。

class ThingieAdmin(admin.ModelAdmin):
    list_display = ('thingie_name', 'interesting_property', 'computed_property',)

    def computed_property(self, obj):
        # Do anything here
        return value

关于django - 如何将计算列添加到 django 模型的列表页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29616593/

相关文章:

python - 如何在python中将元素添加到已排序的redis集中

c++ - 无法推断出作为函数的模板参数

Android自定义View(TextView+Button+一些自定义行为)?

django - 使用 url 作为源的 jQueryUI 自动完成(我使用 Django)

django - 从 AbstractUser 继承的模型不会散列密码字段

python - Django 要求定义 `model`,而它已经定义了

mysql - 选择行计数用作任意行 ID

c++ - 我收到有关我的模板类的错误

python - 如何在Cheetah for Python中正确使用多维字典?

xcode - 当 View 被关闭时,呈现的 View 会抛出 EXC_BAD_ACCESS