[Django]-How to change Django Admin Custom list field label?

71👍

✅

I guess you should use short_description attribute. Django-admin

def my_stock(self):
    return self.stock
my_stock.short_description = 'Your label here'

5👍

change your model and verbose_name parametr to field. I change "author" label to "Author(s)"

models.py

class books(models.Model):
    title = models.CharField(max_length = 250)
    author= models.CharField(max_length = 250, verbose_name='Author(s)')

or simply

author      = models.CharField('Author(s)',max_length = 250)

2👍

You can create the custom column "my_stock" with my_stock() then can rename it with @admin.display as shown below:

# "models.py"

class Book(models.Model):
    name = models.CharField(u"Нэр", max_length=200)
    stock = models.IntegerField()

    # @property
    # def in_stock(self):
    #     return self.stocks.count()
# "admin.py"

class BookAdmin(admin.ModelAdmin):
    list_display  = ('name', 'my_stock')
    search_fields = ('name', )
                                     # ↓ Displayed 
    @admin.display(description='Your label here')
    def my_stock(self, obj):
        return obj.stock

Leave a comment