Django-polymorphic-tree list_display 来自内容字段的对象名称

Django-polymorphic-tree list_display object name from Contents Fields

我想在 django-admin 站点上显示我的对象名称,但我使用的是 django-polymorphic-tree 并且我的对象名称应该是该对象内容中的文本,我该怎么做?

我已经试过了 __str __() 在多态树子 classes 中,这不起作用,因为 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)

总结一下,我只想添加到 list_display,我的 'to_show' 来自 Node 或 CategoryNode class 的变量。

几天后,我解决了这个问题。反正很简单,你必须这样做:

Models.py:

class UserTimelines(PolymorphicMPTTModel):
    parent = PolymorphicTreeForeignKey('self', null=True, blank=True, related_name='children', db_index=True, on_delete=models.CASCADE)

    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)

    def __str__(self):
        return str(self.node_title)

    class Meta:
        verbose_name = "Node"
        verbose_name_plural = "Nodes"

class CategoryNode(UserTimelines):
    category_username = models.CharField(max_length=25, null=True)

    def __str__(self):
        return str(self.category_username) + ' timetable'

    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 = ('_show', 'actions_column',)

    def _show(self, obj):
        return str(UserTimelines.objects.get(pk=obj.pk).__str__())

    class Media:
    css = {
        'all' : ('admin/treenode/admin.css',)
    }

admin.site.register((Node, CategoryNode),BaseChildAdmin)
admin.site.register(UserTimelines, TreeNodeParentAdmin)

我认为这个解决方案可以帮助将来的某个人,所以给你。