python - 如何检查 list_display 如果没有值,以在 django admin 中显示?

标签 python django list django-admin display

Django 版本 1.10

如何有条件地检查变量是否为空并显示“-”?

即如果get_followers和/或get_following未设置,则显示“-”

@admin.register(Application)
class ApplicationAdmin(admin.ModelAdmin):
  list_display = [
        'name',
        'get_following',
        'get_followers',
        'get_friends',
        'manager',
       ]

最佳答案

这是 list_display 的文档: https://docs.djangoproject.com/en/1.10/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_display

您可以使用方法来实现此目的。例如:

class Application(models.Model):
    name = models.CharField(max_length=100)
    following = ...
    followers = ...
    friends = ...

    def get_following(self):
        if not self.following:
            return "-"
        return self.following

在你的 admin.py 中你只需输入方法的名称

@admin.register(Application)
class ApplicationAdmin(admin.ModelAdmin):

  list_display = [
        'name',
        'get_following',
        ...
       ]

关于python - 如何检查 list_display 如果没有值,以在 django admin 中显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41815246/

相关文章:

python - get_or_create() 是否必须立即保存? ( Django )

python - 从 Pandas 数据框中的列名中删除时间戳

python - Flask-Login 显示 401 而不是重定向到登录 View

python - 可以在 Python 中创建非常大的元组吗?

Python:从键:值对字符串创建嵌套字典

python - 如何使用 matplotlib 绘制热图?

python - 有什么方法可以重命名 django.auth 表吗?

python - 从python速成类(class)Web应用程序Django项目

python - 反转 'index',参数 '()' 和关键字参数 '{}' 未找到。 0 种模式已尝试 : []

list - LISP:仿函数位置的非法参数