Django admin-如何根据其他模型字段值计算字段值 [英] Django admin - How to calculate a field value depending on other model field values

查看:602
本文介绍了Django admin-如何根据其他模型字段值计算字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种情况:
服务维修,可能包括维修/安装机器中的多个备用零件。对于服务维修中涉及的每个备件,技术人员可以选择其状态(新的,二手的和翻新的)。在称为PartCostRate的单独表中,我映射了状态的成本百分比:

I have this scenario: A Service repair that might include repair/installation of multiple spare Parts in a machine. For every spare part involved in the service repair, the technician can select its state (new, used, refurbished). In a separate table called PartCostRate, I map a cost percentage for a state:

models.py

models.py

class Part(models.Model):
    item_code = models.CharField(max_length=255, primary_key=True)
    name = models.CharField('Part Name', max_length=128)
    exw_price = models.DecimalField(max_digits=12, decimal_places=6)

class PartCostRate(models.Model):
    PART_STATES = (
        ('N', 'New'),
        ('U', 'Used'),
        ('R', 'Refurbished'),
    )
    state = models.CharField(max_length=1, choices=Part_STATES)
    rate = models.DecimalField(max_digits=4, decimal_places=2)

class Service(models.Model):
    service_code = models.CharField(max_length=10)

    parts = models.ManyToManyField(Part, through='ServicePart')

class ServicePart(models.Model):
    PART_STATES = (
        ('N', 'New'),
        ('U', 'Used'),
        ('R', 'Refurbished'),
    )
    service = models.ForeignKey(Service)
    part = models.ForeignKey(Part)
    state = models.CharField(max_length=1, choices=PART_STATE)
    quantity = models.IntegerField()
    cost = models.DecimalField(max_digits=12, decimal_places=6)

admin.py

class ServicePartInline(admin.TabularInline):
    model = ServicePart
    extra = 1
    verbose_name = 'Repaired / Installed Part'

class ServiceAdmin(admin.ModelAdmin):
   inlines = [ServicePartInline,]

在管理界面中,如何根据所选内容来计算零件的成本(来自serviceadmin的内联)状态和数量。对于选择二手,我需要查询PartCostRate模型以检查二手率,然后检查该零件的exw_price,然后进行计算而不会忘记数量。

From the admin interface, how can I calculate the cost of the part (from the inline of serviceadmin) depending on the selected state and quantity. For a select of Used, I need to query the PartCostRate model to check what is the rate of Used and then check the exw_price of that part and then do the calculation without forgetting the quantity.

我不确定管理员如何执行此操作。是否需要javascript才能交互显示其字段中的计算成本。因为有时他们可能会手动更改它。

I am not sure how to do that from the admin. Does it require javascript in order to interactivley show the calculated cost in its field. Because sometimes they might change it manually.

预先感谢

推荐答案

有两种方法可以做到

1)像您所说的Javascript。查看此链接。您将看到如何将自己的脚本文件连接到django admin

1) Javascript like you say. Look at this link. You'll see how to hook your own script files into django admin

2)创建您自己的模型形式,扩展ServiceAdmin设置形式的值(签出)表单并更新表单中的值在保存表单时。

2) Create your own modelform, extend your ServiceAdmin setting form value (check this out) form and update the values in form at form save.

这篇关于Django admin-如何根据其他模型字段值计算字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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