在管理界面中显示总金额 [英] Display Total amount in the admin interface

查看:86
本文介绍了在管理界面中显示总金额的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了,没有找到答案。我想在管理模板中获取内联 salesitem 的总数。我希望在添加数量时,该项目的数量*价格在管理内联表中显示。
示例:

I have searched, didn't find any answer. I want to get the total of inline salesitem in the admin template. I want the quantity * price of the item to be shown in the admin inline table as I add quantity. Example:

models.py

class Stock(models.Model):
    price = models.DecimalField()
    quantity = models.PositiveIntegerField()

class Sales(models.Model):
    name = models.CharField()
    #Other fields

class SalesItem(models.Model):
    item = models.ForeignKey(Stock)
    quantity = models.PositiveIntegerField()

admin.py

class SalesItemInline(admin.TabularInline):
    model = SalesItem

class SalesAdmin(admin.ModelAdmin, ExportCsvMixin):
    exclude = ['admin', 'branch']
    inlines = [SalesItemInline]

这是我已经部署的小型清单系统。

It is a mini inventory system that I already deployed.

推荐答案

您可以显示模型函数和属性内联。例如:

You can display model functions and properties in inlines. For example:

class SalesItem(models.Model):
    ...
    @property
    def total(self):
        return self.item.price * self.quantity

,然后将其添加为 readonly_fields 的一部分(因为它是计算属性):

and then add it as part of readonly_fields (because it is a computed property):

class SalesItemInline(admin.TabularInline):
    ...
    fields = ('item', 'quantity', 'total')
    readonly_fields = ('total',)

这篇关于在管理界面中显示总金额的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆