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

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

问题描述

如果django模型包含一个外键字段,并且如果该字段显示在列表模式下,则它显示为文本,而不是显示链接外部对象。



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



(当然可以在现场基础上做这个,但是有没有一般的方法?)



示例

 类作者(models.Model):
...

class Post(models。模型):
author = models.ForeignKey(作者)

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

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

现在在列表模式下,作者字段只会使用作者类的 __ unicode __ 方法显示作者。最重要的是,我想要一个链接,指向管理站点中相应作者的URL。这是可能的吗?



手工方法



为了完整起见,我添加手动方法。在 PostAdmin 类中添加一个方法 author_link

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

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

解决方案

不要以为有自动开箱即用的机制。



但是,根据ID的ID来确定管理员编辑页面的路径一个对象,你需要的是两条信息:



a) self.model._meta.app_label / p>

b) self.model._meta.module_name



然后,例如,要进入你将要执行的模型的编辑页面:

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

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



我想你可以有一个可访问的模型名称和一个id,创建一个指定类型的模型只是为了ge t标签和名称(不需要打数据库)并生成上面的URL。



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



编辑:



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



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



您可以在管理列表视图中执行的操作是在Post模型中创建一个可以使用逗号分隔的作者列表的可调用。所以,您的发布列表视图将显示正确的作者,并且您可以在Post管理界面中直接编辑与帖子相关联的作者。


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?)

Example:

class Author(models.Model):
    ...

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

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

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

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?

Manual method:

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

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.

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)

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

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...

Edit:

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.

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天全站免登陆