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

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

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

        可以“list_display"在 Django ModelAdmin 中显示 ForeignKey 字段的属

        Can quot;list_displayquot; in a Django ModelAdmin display attributes of ForeignKey fields?(可以“list_display在 Django ModelAdmin 中显示 ForeignKey 字段的属性?)
          <tbody id='2xlXp'></tbody>

          <bdo id='2xlXp'></bdo><ul id='2xlXp'></ul>
        • <small id='2xlXp'></small><noframes id='2xlXp'>

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

                  <legend id='2xlXp'><style id='2xlXp'><dir id='2xlXp'><q id='2xlXp'></q></dir></style></legend>
                  本文介绍了可以“list_display"在 Django ModelAdmin 中显示 ForeignKey 字段的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个 Person 模型,它与 Book 有外键关系,它有许多字段,但我最关心的是 author(一个标准的 CharField).

                  I have a Person model that has a foreign key relationship to Book, which has a number of fields, but I'm most concerned about author (a standard CharField).

                  话虽如此,在我的 PersonAdmin 模型中,我想使用 list_display 显示 book.author:

                  With that being said, in my PersonAdmin model, I'd like to display book.author using list_display:

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

                  我已经尝试了所有明显的方法,但似乎没有任何效果.

                  I've tried all of the obvious methods for doing so, but nothing seems to work.

                  有什么建议吗?

                  推荐答案

                  作为另一种选择,您可以进行如下查找:

                  As another option, you can do look ups like:

                  class UserAdmin(admin.ModelAdmin):
                      list_display = (..., 'get_author')
                      
                      def get_author(self, obj):
                          return obj.book.author
                      get_author.short_description = 'Author'
                      get_author.admin_order_field = 'book__author'
                  

                  从 Django 3.2 开始,您可以使用 display() 装饰器:

                  Since Django 3.2 you can use display() decorator:

                  class UserAdmin(admin.ModelAdmin):
                      list_display = (..., 'get_author')
                      
                      @display(ordering='book__author', description='Author')
                      def get_author(self, obj):
                          return obj.book.author
                  

                  这篇关于可以“list_display"在 Django ModelAdmin 中显示 ForeignKey 字段的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
                  Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
                  Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
                  Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
                  Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)
                  Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)
                1. <legend id='3LdMn'><style id='3LdMn'><dir id='3LdMn'><q id='3LdMn'></q></dir></style></legend>

                  <tfoot id='3LdMn'></tfoot>
                    <bdo id='3LdMn'></bdo><ul id='3LdMn'></ul>

                          <small id='3LdMn'></small><noframes id='3LdMn'>

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