Django - Admin: list_display TextField

I am trying to map the first 10 characters of a TextField to list_display .
Is this possible in the admin interface?

+6
source share
2 answers

You can define a callable that returns the first 10 characters of the field, and add this to list_display .

See the Django list_display for list_display for more list_display .

+13
source
 myapp/admin.py from django.contrib import admin from django.utils.text import Truncator from django.db import models from .models import Product def truncated_name(obj): name = "%s" % obj.name return Truncator(name).chars(70) class ProductAdmin(admin.ModelAdmin): list_display = ['id', truncated_name, 'category', 'timestamp',] list_display_links = [truncated_name] list_filter = ['category'] class Meta: model = Product 

You can also override these fields:

  formfield_overrides = { models.CharField: {'widget': TextInput(attrs={'size': '20'})}, models.TextField: {'widget': Textarea(attrs={'rows': 1, 'cols': 40, 'style': 'height: 1.5em;'})}, } 
0
source

Source: https://habr.com/ru/post/911922/


All Articles