且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Django Admin 链接到相关对象

更新时间:2023-11-30 16:23:40

将此添加到您的模型中:

Add this to your model:

  def user_link(self):
      return '<a href="%s">%s</a>' % (reverse("admin:auth_user_change", args=(self.user.id,)) , escape(self.user))

  user_link.allow_tags = True
  user_link.short_description = "User" 

您可能还需要将以下内容添加到 models.py 的顶部:

You might also need to add the following to the top of models.py:

  from django.template.defaultfilters import escape
  from django.core.urls import reverse

admin.py中,在list_display中,添加user_link:

list_display = ('name', 'user_link', )

不需要 list_display_links.