• <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:按相关外键的值排序

        时间:2023-11-07
        <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:按相关外键的值排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 Django 管理更改/添加页面中显示 ForeignKey 数据 下一篇:在表“django_admin_log"上插入或更新在管理员中保存新模型时违反外键约束

                  相关文章

                  <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>
                  1. <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>