Search code examples
djangolistadmindisplay

django admin truncate text in list_display


Need to trunate text in admin list_display

Have the following in admin model but still shows full text.

from django.template.defaultfilters import truncatewords

def get_description(self, obj):
    return truncatewords(obj.description, 10)
get_description.short_description = "description"

class DieTaskAdmin(admin.ModelAdmin):
    list_display =['severity','priority', 'subject', 'status','created',get_description.short_description']

admin.site.register(DieTask, DieTaskAdmin)

i.e original text of description field contains more than 255 char. I like to only display the first 10 char plus ...


Solution

  • I had to create a property in the model as shown here:

    from django.template.defaultfilters import truncatechars
    ...
    
    @property
    def short_description(self):
        return truncatechars(self.description, 35)
    

    And use the short_descriptioin in the admin to trim the text.