Django:allow_tags 和 short_description 如何工作?

bin*_*npy 1 python django django-models django-admin

在 中执行此操作的基本方法models.py,例如:

class Foo(models.Model):
    title = models.CharField(max_length=200)
    ...

    def custom_tag(self):
        return ('custom: %s' % self.title)
    custom_tag.allow_tags = True
    custom_tag.short_description = _("Custom Tag")
Run Code Online (Sandbox Code Playgroud)

或者,如果在admin.py;

class FooAdmin(admin.ModelAdmin):
    list_display = ['title', 'custom_tag', ...]
    ...

    def custom_tag(self, instance):
        return ('custom: %s' % instance.title)
    custom_tag.allow_tags = True
    custom_tag.short_description = _("Custom Tag")


admin.site.register(Foo, FooAdmin)
Run Code Online (Sandbox Code Playgroud)

我的问题是,如何做allow_tags和short_description工作?我在哪里可以找到相关文件?

我无法在文档或源代码中找到它

Ano*_*ous 5

您正在查看文档的开发版本。如果您查看Django 1.10 的那个(向下滚动到“自 1.9 版起已弃用”),您会看到他们正在删除该allow_tags选项并用其他实现相同目的的方法替换它。有很多关于如何使用的例子,short_description因为它没有被弃用。

如果您真的想查看源代码,这里是获取简短说明的行。不用担心,allow_tags因为它在 1.11 中被删除了 - 现在应该通过使用 将字符串标记为安全来自动完成mark_safe()。


作为旁注,您不需要custom_tag()在两个地方都添加该方法。管理员在模型和管理类中查找它,所以一个就足够了。如果不打算在 admin 之外使用它,我建议将它放在 admin 类中并避免使模型更复杂。