如何从一个对象的Django管理页面添加到相关对象的管理页面的链接? [英] How do I add a link from the Django admin page of one object to the admin page of a related object?
问题描述
要处理django-admin中的缺少嵌套内联,我把特殊情况放在两个模板中,以创建管理员更改页面和两个模型的内联管理员之间的链接。
我的问题是:如何我创建一个从管理员更改页面或一个模型的内联管理员链接到管理员更改页面或相关模型的内联管理员干净,没有讨厌的黑客在模板中
我想要一个一般的解决方案,我可以应用于任何型号的管理员更改页面或内联管理员。
我有一个模型,发布
(不是真实姓名),它们都是博客上的内联
管理员页面,还有自己的管理页面。它不能只是内联的原因是,它具有外键的模型,只有在使用它进行编辑时才有意义,并且只有在使用博客
进行编辑时才有意义
对于发布
管理页面,我更改了fieldset.html的一部分:
{%if field.is_readonly%}
< p> {{field.contents}}< / p>
{%else%}
{{field.field}}
{%endif%}
to
{%if field.is_readonly%}
/ pre>
< p> { {field.contents}}< / p>
{%else%}
{%ifequal field.field.nameblog%}
< p> {{field.field.form.instance.blog_link | safe}}< ; / p为H.
{%else%}
{{field.field}}
{%endifequal%}
{%endif%}
创建指向
博客
管理页面的链接,其中blog_link
是模型上的一种方法:def blog_link(self):
return' a href =%s>%s< / a>'%(reverse(admin:myblog_blog_change,
args =(self.blog.id,)),escape(self.blog))
我找不到
id
博客
实例以外任何地方field.field.form.instance
。
在
博客
管理页面上,其中发布
是内联的,我修改了stacking.html的一部分:< h3>< b> {{inline_admin_formset.opts.verbose_name | title}}:< / b> &安培; NBSP;
< span class =inline_label> {%if inline_admin_form.original%}
{{inline_admin_form.original}}
{%else%}#{{forloop.counter}} {%endif%}< / span>
至
< h3>< b> {{inline_admin_formset.opts.verbose_name | title}}:< / b>& nbsp;
< span class =inline_label> {%if inline_admin_form.original%}
{%ifequal inline_admin_formset.opts.verbose_namepost%}
< a href =/ admin / myblog / post / {{inline_admin_form.pk_field.field.value}} />
{{inline_admin_form.original}}< / a>
{%else%} {{inline_admin_form.original}} {%endifequal%}
{%else%}#{{forloop.counter}} {%endif%}< / span>
创建一个链接到
post
admin页面,因为这里我可以找到存储在外键字段中的id
。
我确定有一个更好的,更一般的方式来添加链接到管理表单而不重复自己;这是什么?
解决方案Django 1.8中的新功能: show_change_link for inline admin 。
在您的内联模型中将 show_change_link 设置为 True (默认为False),以便内联对象具有指向其更改形式的链接(可以在其中拥有自己的inline)
from django.contrib import admin
class PostInline(admin.StackedInline):
model = Post
show_change_link = True
...
class BlogAdmin(admin.ModelAdmin):
inlines = [PostInline]
。
class ImageInline(admin.StackedInline):
#假设图像模型有外键要发布
model = Image
show_change_link = True
。 ..
class PostAdmin(admin.ModelAdmin):
inlines = [I mageInline]
...
admin.site.register(Blog,BlogAdmin)
admin.site.register(Post,PostAdmin)
To deal with the lack of nested inlines in django-admin, I've put special cases into two of the templates to create links between the admin change pages and inline admins of two models.
My question is: how do I create a link from the admin change page or inline admin of one model to the admin change page or inline admin of a related model cleanly, without nasty hacks in the template?
I would like a general solution that I can apply to the admin change page or inline admin of any model.
I have one model,
post
(not its real name) that is both an inline on theblog
admin page, and also has its own admin page. The reason it can't just be inline is that it has models with foreign keys to it that only make sense when edited with it, and it only makes sense when edited withblog
.For the
post
admin page, I changed part of "fieldset.html" from:{% if field.is_readonly %} <p>{{ field.contents }}</p> {% else %} {{ field.field }} {% endif %}
to
{% if field.is_readonly %} <p>{{ field.contents }}</p> {% else %} {% ifequal field.field.name "blog" %} <p>{{ field.field.form.instance.blog_link|safe }}</p> {% else %} {{ field.field }} {% endifequal %} {% endif %}
to create a link to the
blog
admin page, whereblog_link
is a method on the model:def blog_link(self): return '<a href="%s">%s</a>' % (reverse("admin:myblog_blog_change", args=(self.blog.id,)), escape(self.blog))
I couldn't find the
id
of theblog
instance anywhere outsidefield.field.form.instance
.On the
blog
admin page, wherepost
is inline, I modified part of "stacked.html" from:<h3><b>{{ inline_admin_formset.opts.verbose_name|title }}:</b> <span class="inline_label">{% if inline_admin_form.original %} {{ inline_admin_form.original }} {% else %}#{{ forloop.counter }}{% endif %}</span>
to
<h3><b>{{ inline_admin_formset.opts.verbose_name|title }}:</b> <span class="inline_label">{% if inline_admin_form.original %} {% ifequal inline_admin_formset.opts.verbose_name "post" %} <a href="/admin/myblog/post/{{ inline_admin_form.pk_field.field.value }}/"> {{ inline_admin_form.original }}</a> {% else %}{{ inline_admin_form.original }}{% endifequal %} {% else %}#{{ forloop.counter }}{% endif %}</span>
to create a link to the
post
admin page since here I was able to find theid
stored in the foreign key field.
I'm sure there is a better, more general way to do add links to admin forms without repeating myself; what is it?
解决方案New in Django 1.8 : show_change_link for inline admin.
Set show_change_link to True (False by default) in your inline model, so that inline objects have a link to their change form (where they can have their own inlines).
from django.contrib import admin class PostInline(admin.StackedInline): model = Post show_change_link = True ... class BlogAdmin(admin.ModelAdmin): inlines = [PostInline] ... class ImageInline(admin.StackedInline): # Assume Image model has foreign key to Post model = Image show_change_link = True ... class PostAdmin(admin.ModelAdmin): inlines = [ImageInline] ... admin.site.register(Blog, BlogAdmin) admin.site.register(Post, PostAdmin)
这篇关于如何从一个对象的Django管理页面添加到相关对象的管理页面的链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!