答案 0 :(得分:0)
如果您在方法中定义内容,则可以将自定义HTML作为字段添加到管理员中:
from django.utils.html import format_html
from django.utils.safestring import mark_safe
@admin.register(MyModel)
class MyModelAdmin(ModelAdmin):
fields = ['related_thing_links', ...]
readonly_fields = ['related_thing_links']
def related_thing_links(self, obj):
items = []
for thing in obj.related_things.all():
url = reverse(
'admin:myapp_relatedthing_change',
kwargs={'pk': thing.pk}
)
items.append(format_html(
'<li><a href="{url}">{thing}</a></li>',
url=url,
thing=thing,
))
return mark_safe('<ul>' + ' '.join(items) + '</ul>')
related_thing_links.short_description = 'related things'
我没有设法完全测试,但它基于工作代码。