I am trying to sort a Django administrators list page by a specific value in a corresponding external set of objects.
In particular, in the code below, I want the ContentAdmin view to display a list of all content objects sorted by "Twitter Score" value (Score object named "Twitter").
In a django application, 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')
And in 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)
PURPOSE: Admin panel for ContentAdmin displays content objects sorted by Twitter account
Thanks everyone!
djs22 source
share