Home > OS >  why Django admin short_description function not working?
why Django admin short_description function not working?

Time:04-09

I am trying to make some changes in my django admin panel such as want to show "title" instead of "blog_tile" but I am not understanding why changes not reflecting.

class BlogAdmin(admin.ModelAdmin):
    readonly_fields = ['blog_publish_time', 'blog_update_time']
    list_display = ['blog_title', 'blog_status',
                    'blog_publish_time', 'blog_update_time']

    def rename_blog_title(self, obj):
        return obj.blog_title[:10]
    rename_blog_title.short_description = "title"


admin.site.register(Blog, BlogAdmin)

where I am doing mistake?

CodePudding user response:

You are using blog_title, not rename_blog_title in your list_display. You thus should refer to the method, not to the field of your Blog model:

class BlogAdmin(admin.ModelAdmin):
    readonly_fields = ['blog_publish_time', 'blog_update_time']
    list_display = ['rename_blog_title', 'blog_status', 'blog_publish_time', 'blog_update_time']

    def rename_blog_title(self, obj):
        return obj.blog_title[:10]
    rename_blog_title.short_description = 'title'

admin.site.register(Blog, BlogAdmin)
  • Related