开发者

django, show fields on admin site that are not in model

开发者 https://www.devze.com 2023-01-11 13:46 出处:网络
Building a Django app. Class Company(models.Model): trucks = models.IntegerField() multiplier = models.IntegerField()

Building a Django app.

Class Company(models.Model):
    trucks = models.IntegerField()
    multiplier = models.IntegerField()
    #capacity = models.IntegerField()

The 'capacity' field is actually the sum of (trucks * multiplier). So I don't开发者_如何学Go need a database field for it since I can calculate it.

However, my admin user wants to see this total on the admin screen.

What's the easiest/best way to customize the admin view to allow me to do this?


define a method on company that returns the product of trucks and multiplier

def capacity(self):
    return self.trucks * self.multiplier

and in the admin model set

list_display = ('trucks', 'multiplier', 'capacity')
0

精彩评论

暂无评论...
验证码 换一张
取 消