As you say, a function definition is the path to a user method of the ModelAdmin class, which takes an object as a parameter and returns a string representation of the last comments:
class ArticleAdmin(admin.ModelAdmin):
list_display = ('name', 'latest_comments')
def latest_comments(self, obj):
return '<br/>'.join(c.comment for c in obj.comment_set.order_by('-date')[:3])
latest_comments.allow_tags = True
This takes the last three comments for each article, sorted by date field, and displays commenteach field separated by an HTML tag <br>to display on each line.
source
share