1

我想在 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。

4

1 回答 1

0

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

模型.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"

管理员.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)

我认为这个解决方案可以在未来帮助某人,所以你在这里。

于 2019-07-28T17:29:20.620 回答