varad
varad

Reputation: 8029

django admin truncate words in list display

I am returning a text field in admin list display and it is too long so i want to truncate it..

def comment(self, obj):
    return '{{{}|truncatewords:10}}'.format(obj.comment)
comment.allow_tags = True
comment.short_description = "Comment"

It is not giving me the comment. I have included the comment in list_display Any suggestions ?

Upvotes: 1

Views: 957

Answers (1)

Hedde van der Heide
Hedde van der Heide

Reputation: 22449

You cannot duplicate the attribute's name, use a custom definition instead, e.g.

def get_comment(self, obj):
    return ...
get_comment.allow_tags = True
get_comment.short_description = "Comment"

Also you should use truncatewords as a function when not using in inside a template

from django.template.defaultfilters import truncatewords

def get_comment(self, obj):
    return truncatewords(obj.comment, 10)
get_comment.allow_tags = True
get_comment.short_description = "Comment"

Upvotes: 2

Related Questions