Reputation: 393
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
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,]
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.
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.
Thanks in advance
Upvotes: 2
Views: 3092
Reputation: 5656
There are two ways to do it
1) Javascript like you say. Look at this link. You'll see how to hook your own script files into django admin
2) Create your own modelform, extend your ServiceAdmin setting form value (check this out) form and update the values in form at form save.
Upvotes: 1