• <small id='x9Sib'></small><noframes id='x9Sib'>

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

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

        Django Admin:按相关外键的值排序

        Django Admin: Order by value on related Foreign Key(Django Admin:按相关外键的值排序)
        <legend id='0J1Mc'><style id='0J1Mc'><dir id='0J1Mc'><q id='0J1Mc'></q></dir></style></legend>

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

              • <bdo id='0J1Mc'></bdo><ul id='0J1Mc'></ul>

                  本文介绍了Django Admin:按相关外键的值排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试按对象相关外键集中的特定值对 Django 管理列表页面进行排序.

                  I'm trying to sort a Django Admin list page by a specific value in the objects' related foreign key set.

                  具体来说,在下面的代码中,我希望 ContentAdmin 视图显示按Twitter Score"排序的所有内容对象的列表(名为Twitter"的 Score 对象).

                  Specifically, in the below code, I want the ContentAdmin view to show a list of all content objects sorted by the "Twitter Score" (The Score object with name "Twitter").

                  在 django 应用中,我有以下模型:

                  In the django app I have the following models:

                  class Content(models.Model):
                      body = models.CharField(max_length=564)
                      title = models.CharField(max_length=64) 
                  
                  class Score(models.Model):
                      name = models.CharField(max_length=64)
                      score = models.IntegerField()
                      content = models.ForeignKey('Content')
                  

                  在 admin.py 我有以下内容:

                  And in the admin.py I have the following:

                  class ContentAdmin(admin.ModelAdmin):
                      list_display = ('title', 'show_twitter_score',)
                  
                      def show_twitter_score(self, obj):
                          twitter_score = obj.score_set.get(name='Twitter')
                          return 'Twitter: ' + str(twitter_score.score)
                  

                  目标:ContentAdmin 的管理面板显示按Twitter"分数排序的内容对象

                  GOAL: The admin panel for ContentAdmin displays the content objects ordered by "Twitter" scores

                  谢谢大家!

                  推荐答案

                  我通过扩展 ContentAdmin 类的 get_queryset 方法解决了这个问题.之后,只需获取正确的 ORM 查询即可

                  I solved this by extending the get_queryset method of the ContentAdmin class. After that, it was just a matter of getting the right ORM query

                  def get_queryset(self, request):
                      qs = super(ContentAdmin, self).get_queryset(request)
                      return qs.filter(score__name='Twitter').order_by('-score__score')
                  

                  对于 Django 1.5 及更早版本,方法是 queryset.

                  For Django 1.5 and earlier, the method was queryset.

                  def queryset(self, request):
                      qs = super(ContentAdmin, self).queryset(request)
                      return qs.filter(score__name='Twitter').order_by('-score__score')
                  

                  这篇关于Django Admin:按相关外键的值排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  What happens when you compare 2 pandas Series(当你比较 2 个 pandas 系列时会发生什么)
                  Quickly find differences between two large text files(快速查找两个大文本文件之间的差异)
                  Python - Compare 2 files and output differences(Python - 比较 2 个文件和输出差异)
                  Why do comparisions between very large float values fail in python?(为什么在 python 中非常大的浮点值之间的比较会失败?)
                  Dictionary merge by updating but not overwriting if value exists(字典通过更新合并,但如果值存在则不覆盖)
                  Find entries of one text file in another file in python(在python中的另一个文件中查找一个文本文件的条目)
                  <i id='7mvPB'><tr id='7mvPB'><dt id='7mvPB'><q id='7mvPB'><span id='7mvPB'><b id='7mvPB'><form id='7mvPB'><ins id='7mvPB'></ins><ul id='7mvPB'></ul><sub id='7mvPB'></sub></form><legend id='7mvPB'></legend><bdo id='7mvPB'><pre id='7mvPB'><center id='7mvPB'></center></pre></bdo></b><th id='7mvPB'></th></span></q></dt></tr></i><div id='7mvPB'><tfoot id='7mvPB'></tfoot><dl id='7mvPB'><fieldset id='7mvPB'></fieldset></dl></div>

                  <tfoot id='7mvPB'></tfoot>
                    <tbody id='7mvPB'></tbody>

                      <small id='7mvPB'></small><noframes id='7mvPB'>

                        <bdo id='7mvPB'></bdo><ul id='7mvPB'></ul>

                            <legend id='7mvPB'><style id='7mvPB'><dir id='7mvPB'><q id='7mvPB'></q></dir></style></legend>