django - 'staticmethod' 对象没有属性 'short_description'

标签 django django-admin pylint

我正在写一个非常普通的 django 管理类,带有这样的自定义操作:

class DeviceAdmin(admin.ModelAdmin):
    actions = ("enable", "disable")

    def enable(self, request, queryset):
        queryset.update(active=True)
    enable.short_description = _("Enable selected items")

    def disable(self, request, queryset):
        queryset.update(active=False)
    disable.short_description = _("Disable selected items")

现在 pylint 提示 self 没有在 enabledisable 中使用,并且这些方法可以是函数(或静态方法)

添加 @staticmethod 装饰器会导致 django 出错。

AttributeError: 'staticmethod' object has no attribute 'short_description'

我的问题是,如何让 django 和 pylint 都开心?

最佳答案

我也发现了这个问题,但是,我阅读了 django 代码。你可以阅读 django.contrib.admin.options #1270,我们定义了一个 admin func 不是类的“真实”方法,django 代码使用 self.get_actions(request)[action][0] 获取你的函数,然后,使用 func(self, request, queryset) 运行它,所以,你不能使用 @staticmethod解决pylint警告

关于django - 'staticmethod' 对象没有属性 'short_description',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37019978/

相关文章:

python - Django 迁移错误 : ERRORS: 'choices' must be an iterable (e. g.,列表或元组)

python - Django 1.7 : how to make ManyToManyField required?

python - Django Admin- 限制外键字段的选择

python - 在不抑制和操纵 pylint 设置的情况下控制 "too many local variable in a function"的最佳做法是什么?

python - 皮林特最佳实践

pycharm - 如何使用 pyproject.toml 为 PyCharm 配置 pylint?

python - 在 Django 模板中, `Context` 是一个堆栈。做什么的?

使用 _set.all 的 Django 模板

Django:获取相关对象的相关对象并传递给模板

python - 如何在 django admin 中显示其他模型的只读数据?