假设我有一些Django模型,它有一个数字列表作为字段。我使用模型中的自定义方法计算了该字段的一些统计信息。这些模型在管理面板上显示为父对象中“嵌入”的InlineAdminModel。有没有办法在修改父模型时在管理面板中显示此自定义方法值,这显然只适用于已创建的模型。
e.g。 (截短的)
class ListModel(models.Model):
number_list = models.CommaSeparatedIntegerField(max_length = 20)
def sum(self):
return sum([int(s) for s in number_list.split(',')])
class ListModelAdmin(admin.TabularInline):
model = ListModel
list_display = ('sum') #works fine for displaying the ListModel
fields = ['number_list', 'sum'] #Obviously Doesn't work, but demonstrates
#what I want to accomplish. sum wouldn't be settable but displayed when
#number_list is updated