Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
330 views
in Technique[技术] by (71.8m points)

Django admin listview Customize Column Name

Ok so I have a custom django admin built from a Author Model:

class AuthorAdmin(admin.ModelAdmin):
    """
    Author Admin
    """
    form = AuthorForm

    list_display = ['profile_photo', 'first_name', 'last_name', 'title']
    search_fields = ['first_name', 'last_name', 'title', 'credential']
    prepopulated_fields = {'slug': ('first_name', 'last_name', 'title')}

    def profile_photo(self, obj) :
        return '<img src="%s" title="%s" />' % (resize_image(obj.photo, '100x100'), obj.title)

    profile_photo.allow_tags = True

But in the django admin listview the column title for the custom column does not have proper capitalization. capitalization matters dammit!

Does anyone know how to override the column headers that are built from custom function's names?

I've tried:

def my_function(self, obj) :
    """My Custom Title"""
    ...

and

def my_function(self, obj) :
    class Meta:
        verbose_name = _(u"My Custom Title")
question from:https://stackoverflow.com/questions/9708455/django-admin-listview-customize-column-name

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use:

def my_function(self, obj) :
    """My Custom Title"""
    ...
my_function.short_description = 'This is the Column Name'

It's buried in the admin docs. short_description, specifically, is barely mentioned under the discussion of list_display (more by example than actually called out). The other items like this are similiarly buried in the admin docs, but here's a summary:

  • short_description: the column title to use (string)
  • allow_tags: what the name says... let's you use HTML (True or False)
  • admin_order_field: a field on the model to order this column by (string, field name)
  • boolean: indicates the return value is boolean and signals the admin to use the nice graphic green check/red X (True or False)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...