2
我有这种情况: 服务修复可能包括修复/安装一台机器中的多个备件。对于服务维修中涉及的每一个备件,技术人员可以选择其状态(新的,旧的,翻新的)。在所谓的PartCostRate一个单独的表,我映射成本百分比的状态:Django admin - 如何根据其他模型字段值计算字段值
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的内联)取决于所选的状态和数量。对于Used的选择,我需要查询PartCostRate模型以检查Used的比率,然后检查该部分的exw_price,然后在不忘记数量的情况下进行计算。
我不知道如何从管理员那里做到这一点。它是否需要JavaScript以便interactivley在其字段中显示计算的成本。因为有时他们可能会手动更改它。
在此先感谢
我是否正确如果我说:我不能更新成本字段值取决于在第二个选项中保存按钮的命中之前所选择的状态?我希望用户看到该值,因为有时(出于某些原因),他们需要在保存之前对其进行更改。如果属实,那么我剩下选项1. – blaise
你是对的。在用户点击“保存”按钮后,所有在save()方法中发生的事情都会完成。但是这并不是一个问题,因为您可以在保存表单后显示相同的视图。尽管django管理员流程的其他部分会发生这种变化,但可能不是一个好的选择:P。 –