Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make django admin to display no more than 100 characters in list results

Tags:

I am using Django admin for my site and I would like to make a customization on how a field is displayed on the list_display page for one of my models.

One of my models has a TextField that can be 300 characters

When the model is listed in the Django admin, I would like to limit the length of the text displayed in the Admin list display to 100 characters.

Is there a way to do this within the Django Admin class?

admin.py:

class ApplicationAdmin(admin.ModelAdmin):     model = Application     list_display = [ "title1", "title2"] 

models.py:

class Application(models.Model):     title1 = models.TextField(max_length=300)     title2 = models.TextField(max_length=300) 
like image 448
user1865341 Avatar asked Mar 08 '13 02:03

user1865341


People also ask

How can I remove extra's from Django admin panel?

Take a look at the Model Meta in the django documentation. Within a Model you can add class Meta this allows additional options for your model which handles things like singular and plural naming. Show activity on this post. inside model.py or inside your customized model file add class meta within a Model Class.


1 Answers

You can display a property that returns a truncated version of your field's value in your ModelAdmin class. Leveraging the built-in template filters makes this easy.

from django.template.defaultfilters import truncatechars  # or truncatewords  class Foo(models.Model):     description = models.TextField()      @property     def short_description(self):         return truncatechars(self.description, 100)  class FooAdmin(admin.ModelAdmin):     list_display = ['short_description'] 
like image 194
Brandon Avatar answered Sep 30 '22 17:09

Brandon