• <bdo id='rfVmr'></bdo><ul id='rfVmr'></ul>
    <tfoot id='rfVmr'></tfoot>

    1. <legend id='rfVmr'><style id='rfVmr'><dir id='rfVmr'><q id='rfVmr'></q></dir></style></legend>

      1. <i id='rfVmr'><tr id='rfVmr'><dt id='rfVmr'><q id='rfVmr'><span id='rfVmr'><b id='rfVmr'><form id='rfVmr'><ins id='rfVmr'></ins><ul id='rfVmr'></ul><sub id='rfVmr'></sub></form><legend id='rfVmr'></legend><bdo id='rfVmr'><pre id='rfVmr'><center id='rfVmr'></center></pre></bdo></b><th id='rfVmr'></th></span></q></dt></tr></i><div id='rfVmr'><tfoot id='rfVmr'></tfoot><dl id='rfVmr'><fieldset id='rfVmr'></fieldset></dl></div>

        <small id='rfVmr'></small><noframes id='rfVmr'>

        如何将一个对象的 Django 管理页面的链接添加到相关对象的管理页面?

        时间:2023-11-08

        1. <legend id='sSSXt'><style id='sSSXt'><dir id='sSSXt'><q id='sSSXt'></q></dir></style></legend>

          <i id='sSSXt'><tr id='sSSXt'><dt id='sSSXt'><q id='sSSXt'><span id='sSSXt'><b id='sSSXt'><form id='sSSXt'><ins id='sSSXt'></ins><ul id='sSSXt'></ul><sub id='sSSXt'></sub></form><legend id='sSSXt'></legend><bdo id='sSSXt'><pre id='sSSXt'><center id='sSSXt'></center></pre></bdo></b><th id='sSSXt'></th></span></q></dt></tr></i><div id='sSSXt'><tfoot id='sSSXt'></tfoot><dl id='sSSXt'><fieldset id='sSSXt'></fieldset></dl></div>

                <tbody id='sSSXt'></tbody>
            • <small id='sSSXt'></small><noframes id='sSSXt'>

              • <bdo id='sSSXt'></bdo><ul id='sSSXt'></ul>
                <tfoot id='sSSXt'></tfoot>

                  本文介绍了如何将一个对象的 Django 管理页面的链接添加到相关对象的管理页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  为了处理 django-admin 中的缺少嵌套内联,我在其中两个模板中加入了特殊情况,以在两个模型的管理员更改页面和内联管理员之间创建链接.

                  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.

                  我的问题是:如何干净利落地创建从一个模型的管理员更改页面或内联管理员到相关模型的管理员更改页面或内联管理员的链接,而不会在模板中出现讨厌的黑客攻击?

                  我想要一个通用的解决方案,可以应用于任何模型的管理员更改页面或内联管理员.

                  I would like a general solution that I can apply to the admin change page or inline admin of any model.

                  我有一个模型,post(不是它的真名),它既是 blog 管理页面上的内联,也有自己的管理页面.它不能只是内联的原因是它具有带有外键的模型,只有在使用它编辑时才有意义,并且只有在使用 blog 编辑时才有意义.

                  I have one model, post (not its real name) that is both an inline on the blog 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 with blog.

                  对于 post 管理页面,我将fieldset.html"的一部分从:

                  For the post admin page, I changed part of "fieldset.html" from:

                  {% if field.is_readonly %}
                      <p>{{ field.contents }}</p>
                  {% else %}
                      {{ field.field }}
                  {% endif %}
                  

                  {% 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 %}
                  

                  创建一个到 blog 管理页面的链接,其中 blog_link 是模型上的一个方法:

                  to create a link to the blog admin page, where blog_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))
                  

                  我在 field.field.form.instance 之外的任何地方都找不到 blog 实例的 id.

                  I couldn't find the id of the blog instance anywhere outside field.field.form.instance.

                  blog 管理页面上,其中 post 是内联的,我从以下位置修改了stacked.html"的一部分:

                  On the blog admin page, where post is inline, I modified part of "stacked.html" from:

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

                  创建一个到 post 管理页面的链接,因为在这里我能够找到存储在外键字段中的 id.

                  to create a link to the post admin page since here I was able to find the id stored in the foreign key field.

                  我确信有一种更好、更通用的方法可以在不重复自己的情况下添加管理表单的链接;这是什么?

                  推荐答案

                  Django 1.8 中的新功能:show_change_link 用于内联管理员.

                  New in Django 1.8 : show_change_link for inline admin.

                  在内联模型中将 show_change_link 设置为 True(默认为 False),以便内联对象具有指向其更改表单的链接(它们可以有自己的内联).

                  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 管理页面的链接添加到相关对象的管理页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  <tfoot id='N5j05'></tfoot>

                1. <i id='N5j05'><tr id='N5j05'><dt id='N5j05'><q id='N5j05'><span id='N5j05'><b id='N5j05'><form id='N5j05'><ins id='N5j05'></ins><ul id='N5j05'></ul><sub id='N5j05'></sub></form><legend id='N5j05'></legend><bdo id='N5j05'><pre id='N5j05'><center id='N5j05'></center></pre></bdo></b><th id='N5j05'></th></span></q></dt></tr></i><div id='N5j05'><tfoot id='N5j05'></tfoot><dl id='N5j05'><fieldset id='N5j05'></fieldset></dl></div>

                  • <bdo id='N5j05'></bdo><ul id='N5j05'></ul>
                        <tbody id='N5j05'></tbody>

                      <small id='N5j05'></small><noframes id='N5j05'>

                        1. <legend id='N5j05'><style id='N5j05'><dir id='N5j05'><q id='N5j05'></q></dir></style></legend>