我想在 django-admin 站点上显示我的对象的名称,但是我正在使用 django-polymorphic-tree 并且我的对象的名称应该是该对象内容中的文本,我该怎么做?
我已经在多态树子类中尝试过 __str __() ,但这不起作用,因为 django-polymorphic-tree 在 admin.py 中使用 list_display 而没有别的。
这是我的models.py的一部分
class UserTimelines(PolymorphicMPTTModel):
parent = PolymorphicTreeForeignKey('self', null=True, blank=True, related_name='children', db_index=True, on_delete=models.CASCADE)
to_show = ''
class Meta(PolymorphicMPTTModel.Meta):
verbose_name = "User Timeline"
verbose_name_plural = "User Timelines"
class Node(UserTimelines):
node_title = models.CharField(max_length=20, null=True)
to_show = str(node_title)
class Meta:
verbose_name = "Node"
verbose_name_plural = "Nodes"
class CategoryNode(UserTimelines):
category_username = models.CharField(max_length=25, null=True)
to_show = str(category_username) + ' timeline'
class Meta:
verbose_name = "Category node"
verbose_name_plural = "Category nodes"
和 admin.py 的一部分
class BaseChildAdmin(PolymorphicMPTTChildModelAdmin):
GENERAL_FIELDSET = ( None, {
'fields' : ('parent',),
})
base_model = UserTimelines
base_fieldsets = (
GENERAL_FIELDSET,
)
class TreeNodeParentAdmin(PolymorphicMPTTParentModelAdmin):
base_model = UserTimelines
child_models = (
(Node),
(CategoryNode),
)
list_display = ('to_show', 'actions_column',)
class Media:
css = {
'all' : ('admin/treenode/admin.css',)
}
admin.site.register((Node, CategoryNode),BaseChildAdmin)
admin.site.register(UserTimelines, TreeNodeParentAdmin)
总结一下,我只想将 Node 或 CategoryNode 类中的“to_show”变量添加到 list_display。