django 管理列表中的外键显示 [英] Foreign keys in django admin list display

查看:21
本文介绍了django 管理列表中的外键显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果 django 模型包含外键字段,并且该字段以列表模式显示,则它显示为 文本,而不是显示 链接异物.

If a django model contains a foreign key field, and if that field is shown in list mode, then it shows up as text, instead of displaying a link to the foreign object.

是否可以自动将所有外键显示为链接而不是纯文本?

Is it possible to automatically display all foreign keys as links instead of flat text?

(当然可以逐个字段进行,但有通用方法吗?)

(of course it is possible to do that on a field by field basis, but is there a general method?)

示例:

class Author(models.Model):
    ...

class Post(models.Model):
    author = models.ForeignKey(Author)

现在我选择一个 ModelAdmin,以便作者以列表模式显示:

Now I choose a ModelAdmin such that the author shows up in list mode:

class PostAdmin(admin.ModelAdmin):
    list_display = [..., 'author',...]

现在在列表模式下,作者字段将只使用 Author 类的 __unicode__ 方法来显示作者.最重要的是,我想要一个 link 指向管理站点中相应作者的 url.这可能吗?

Now in list mode, the author field will just use the __unicode__ method of the Author class to display the author. On the top of that I would like a link pointing to the url of the corresponding author in the admin site. Is that possible?

手动方法:

为了完整起见,我添加了手动方法.这将是在 PostAdmin 类中添加一个方法 author_link :

For the sake of completeness, I add the manual method. It would be to add a method author_link in the PostAdmin class:

def author_link(self, item):
    return '<a href="../some/path/%d">%s</a>' % (item.id, unicode(item))
author_link.allow_tags = True

这将适用于该特定领域,但这不是我想要的.我想要一个通用的方法来达到同样的效果.(其中一个问题是如何在 django 管理站点中自动找出对象的路径.)

That will work for that particular field but that is not what I want. I want a general method to achieve the same effect. (One of the problems is how to figure out automatically the path to an object in the django admin site.)

推荐答案

我认为没有一种机制可以开箱即用地自动执行您想要的操作.

I don't think there is a mechanism to do what you want automatically out of the box.

但就根据对象的 id 确定管理编辑页面的路径而言,您只需要两条信息:

But as far as determining the path to an admin edit page based on the id of an object, all you need are two pieces of information:

a) self.model._meta.app_label

b) self.model._meta.module_name

然后,例如,转到该模型的编辑页面,您将执行以下操作:

Then, for instance, to go to the edit page for that model you would do:

'../%s_%s_change/%d' % (self.model._meta.app_label, self.model._meta.module_name, item.id)

看看 django.contrib.admin.options.ModelAdmin.get_urls 看看他们是如何做到的.

Take a look at django.contrib.admin.options.ModelAdmin.get_urls to see how they do it.

我想你可以有一个可调用的,它接受一个模型名称和一个 ID,创建一个指定类型的模型只是为了获取标签和名称(无需访问数据库)并生成上面的 URL.

I suppose you could have a callable that takes a model name and an id, creates a model of the specified type just to get the label and name (no need to hit the database) and generates the URL a above.

但是你确定你不能使用内联吗?将所有相关组件都放在一个页面中,这将有助于提供更好的用户界面...

But are you sure you can't get by using inlines? It would make for a better user interface to have all the related components in one page...

内联(链接到文档)允许一个管理界面,用于在一页中显示父子关系,而不是将其分成两页.

Inlines (linked to docs) allow an admin interface to display a parent-child relationship in one page instead of breaking it into two.

在您提供的帖子/作者示例中,使用内联意味着编辑帖子的页面还将显示用于添加/编辑/删除作者的内联表单.对最终用户来说更加自然.

In the Post/Author example you provided, using inlines would mean that the page for editing Posts would also display an inline form for adding/editing/removing Authors. Much more natural to the end user.

您可以在管理列表视图中执行的操作是在 Post 模型中创建一个可调用的对象,该对象将呈现以逗号分隔的作者列表.因此,您的帖子列表视图将显示正确的作者,并且您可以直接在帖子管理界面中编辑与帖子关联的作者.

What you can do in your admin list view is create a callable in the Post model that will render a comma separated list of Authors. So you will have your Post list view showing the proper Authors, and you edit the Authors associated to a Post directly in the Post admin interface.

这篇关于django 管理列表中的外键显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆