在 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")
或者,如果在 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)
我的问题是,allow_tags
和 short_description
是如何工作的?在哪里可以找到相关文档?
我在 documentation 找不到它或者也可以访问 source code
最佳答案
您正在查看文档的开发版本。如果你看一下 Django 1.10 的那个(向下滚动到“自版本 1.9 起已弃用”)您会看到他们正在删除 allow_tags
选项,并将其替换为实现相同目的的其他方法。有很多关于如何使用 short_description
的示例,因为它尚未被弃用。
如果你真的想看源代码,here's the line where it gets the short description 。不必担心 allow_tags
,因为它已在 1.11 中删除 - 现在应该通过使用 mark_safe()
将字符串标记为安全来自动完成。
顺便说一句,您不需要在这两个地方添加 custom_tag()
方法。管理员会在模型和管理类中查找它,因此一个就足够了。如果不打算在管理之外使用它,我建议将其放置在管理类中,并避免使模型变得更加复杂。
关于python - Django:allow_tags和short_description如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42316686/