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

      <legend id='IOneD'><style id='IOneD'><dir id='IOneD'><q id='IOneD'></q></dir></style></legend>

          <bdo id='IOneD'></bdo><ul id='IOneD'></ul>

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

      1. <tfoot id='IOneD'></tfoot>

        如何在 Django 管理面板中列出所有与外键相关的对象?

        时间:2023-11-08

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

        <legend id='c3PG5'><style id='c3PG5'><dir id='c3PG5'><q id='c3PG5'></q></dir></style></legend>

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

            • <tfoot id='c3PG5'></tfoot>
                <tbody id='c3PG5'></tbody>
                <bdo id='c3PG5'></bdo><ul id='c3PG5'></ul>

                • 本文介绍了如何在 Django 管理面板中列出所有与外键相关的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想用 django 管理面板实现一个非常简单的功能,但到目前为止我找不到正确的方法:

                  I would like to implement a very simple feature with the django admin panel but I couldn't find the right way to achieve this so far:

                  模型.py

                  class Author(models.Model):
                      name = models.CharField()
                  
                  class Books(models.Model):
                      title = models.CharField()
                      author = models.ForeignKey(Author)
                  

                  admin.py

                  class AuthorAdmin(admin.ModelAdmin):
                      pass
                  admin.site.register(Author, AuthorAdmin)
                  

                  如何为作者列表概览中的每个项目(作者)添加超链接,以链接到显示特定作者所有书籍的视图?例如:

                  How can I add a hyperlink to every item (Author) in the authors' list overview that links to a view showing all books of the specific author? For Example:

                  • J.K.罗琳(书籍)
                  • J.R.R.托尔金(书籍)

                  books 是指向显示作者所有书籍的网站的超链接.

                  where books is a hyperlink to a site showing all books of the author.

                  推荐答案

                  您正在寻找一个 ModelAdmin.list_filter.

                  设置 list_filter 以激活管理员更改列表页面右侧栏中的过滤器.listfilter 可以是字段名,其中指定的字段应该是 BooleanField、CharField、DateField、DateTimeField、IntegerField、ForeignKey 或 ManyToManyField,例如:

                  Set list_filter to activate filters in the right sidebar of the change list page of the admin. A listfilter can be a field name, where the specified field should be either a BooleanField, CharField, DateField, DateTimeField, IntegerField, ForeignKey or ManyToManyField, for example:

                   # Add a list filter author to BookAdmin.
                   # Now you can filter books by author.
                   class BookAdmin(ModelAdmin):
                      list_filter = ('author', )
                  

                  现在您可以使用@Wolph 建议在作者列表显示中添加一个链接.此链接指向按作者过滤的书单:

                  Now you can use @Wolph suggestion to add a link in the Author list_display. This link points to the book list filtered by author:

                  # Add hyperlinks to AuthorAdmin.
                  # Now you can jump to the book list filtered by autor. 
                  class AuthorAdmin(admin.ModelAdmin):
                      def authors(self):
                          return '<a href="/admin/appname/book/?author__id__exact=%d">%s</a>' % (self.author_id, self.author)
                      authors.allow_tags = True
                  

                  替代方案.要保存点击,您还可以直接指向图书的更改视图:

                  ALTERNATIVE. To save a click you can also point to the change view of a book directly:

                  class Books(models.Model):
                      title = models.CharField()
                      author = models.ForeignKey(Author)
                  
                  def get_admin_url(self):
                      return "/admin/appname/books/%d/" %self.id
                  
                  
                  class BookAdmin(admin.ModelAdmin):
                      def authors(self):
                          html = ""
                          for obj in Books.objects.filter(author__id_exact=self.id):
                              html += '<p><a href="%s">%s</a></p>' %(obj.get_admin_url(), obj.title)
                          return html
                      authors.allow_tags = True
                  
                      list_display = ['title', authors]
                  

                  免责声明:未经测试,但如果您修正错字,它会起作用!:)

                  Disclaimer: Not tested, but if you fix the typo's it'll work! :)

                  请注意,这些链接也可以在管理员的其他位置注入.当您将其添加到小部件时,您可以从更改视图转到更改视图.

                  Note that these links can also be injected at other points in the admin. When you add it to a widget, you can go from change view to change view.

                  这篇关于如何在 Django 管理面板中列出所有与外键相关的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:限制为 django admin 内联显示的条目的查询集 下一篇:Django admin - 如何在自定义管理表单中为多对多字段添加绿色加号

                  相关文章

                  • <bdo id='FxVkg'></bdo><ul id='FxVkg'></ul>

                    <legend id='FxVkg'><style id='FxVkg'><dir id='FxVkg'><q id='FxVkg'></q></dir></style></legend>

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

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