オブジェクトの関連する外部キー セットの特定の値で Django 管理リスト ページを並べ替えようとしています。
具体的には、以下のコードでは、「Twitter スコア」(名前が「Twitter」のスコア オブジェクト) でソートされたすべてのコンテンツ オブジェクトのリストを ContentAdmin ビューに表示する必要があります。
django アプリには、次のモデルがあります。
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には次のものがあります:
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」のスコア順に並べられたコンテンツ オブジェクトが表示されます。
みんな、ありがとう!