user3541631
user3541631

Reputation: 4008

In Django admin to truncate the number of characters for an field

I want in Django admin to truncate the number of characters for a field.

For example, for:

list_display = ('description')

I want to show only the first 20 characters.

Upvotes: 1

Views: 938

Answers (2)

juancarlos
juancarlos

Reputation: 631

You can do that:

In your model or admin class, define a method like this:

def get_description(self):
     return self.description [:20]
get_description.short_description="Description"

And in your admin class:

list_display='get_description',

Upvotes: 1

juancarlos
juancarlos

Reputation: 631

You can define a method to do that for example:

def get_description(self):
       return self.description [:20]
 get_description.short_description = "Description"

And you define list_display=('get_description')

Upvotes: 2

Related Questions