gpt4 book ai didi

python - Django 多态树 list_display 内容字段中的对象名称

转载 作者:太空宇宙 更新时间:2023-11-03 20:35:56 27 4
gpt4 key购买 nike

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

最佳答案

几天后,我为这个问题找到了解决方案。无论如何,这很简单,你必须这样做:

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

我认为这个解决方案可以为将来的某人提供帮助,所以你就在这里。

关于python - Django 多态树 list_display 内容字段中的对象名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57170381/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com