如何更改Django管理员自定义列表字段标签?

t3irkdon  于 2023-01-14  发布在  Go
关注(0)|答案(3)|浏览(165)

如何更改**my_stock详细名称**?
models.py:

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

    @property
    def my_stock(self):
        return self.stock

admin.py:

class BookAdmin(admin.ModelAdmin):
    list_display  = ('name', 'my_stock')
    search_fields = ('name', )
bvpmtnay

bvpmtnay1#

我想你应该使用short_description属性。Django-admin

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

hivapdat2#

将模型和verbose_name参数更改为字段。我将"author"标签更改为"Author(s)"
models.py

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

或者干脆

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

t3psigkw3#

您可以使用my_stock()创建自定义列"my_stock",然后使用@admin. display对其重命名,如下所示:

# "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

相关问题