Admin module - Not able to get total which is sum(qty*rate)

Am using admin interface for a purchase invoice entry.

(models.Model):PurHead and PurDetail
(admin.ModelAdmin): PurHeadAdmin
(admin.TabularInline):PurDetailInline

This is working well in admin module except the total field value.
Need to update the value for total field in PurHead model.
total field is the total amount of purchase which is the sum(qty*rate) from PurDetail model.

I am not aware of how to update the total. Please let me know to to update total value in total field.

‘’’
class PurHead(models.Model):
pur_no = models.AutoField(primary_key=True, verbose_name=‘Purchase #’)
date = models.DateField(default = timezone.now, verbose_name=‘Date’)
# total amount of purchase sum(qty*rate) from PurDetail model
total = models.DecimalField(max_digits=9, decimal_places=2, null=True, default=0, verbose_name=‘Total’)

class PurDetail(models.Model):
pur_no = models.ForeignKey(‘PurHead’, on_delete = models.PROTECT, verbose_name=‘Purchase Inv. No’)
product = models.ForeignKey(‘Stock.Product’, on_delete = models.PROTECT, verbose_name=‘Product’)
qty = models.DecimalField(max_digits=4, decimal_places=1, null=True, verbose_name=‘Quantity’)
rate = models.DecimalField(max_digits=7, decimal_places=2, null=True, verbose_name=‘Purchase Price’)
class Meta:
unique_together = ((‘pur_no’, ‘product’),)

class PurDetailInline(admin.TabularInline):
model = PurDetail
readonly_fields = (‘Amt’,)
fields = (‘product’, ‘qty’, ‘rate’, )

class PurHeadAdmin(admin.ModelAdmin):
list_display = (‘pur_no’, ‘date’, ‘total’,)
inlines = [ PurDetailInline, ]

‘’’
This is working well in admin module except the total field value.
Need to update the value for total field in PurHead model.
total field is the total amount of purchase which is the sum(qty*rate) from PurDetail model.

I am not aware of how to update the total. Please let me know to to update total value in total field.

When you post code snippets, please insert them between lines containing only 3 backticks - ```. (Make sure you use the backtick - ` and not the apostrophe - '.) That means you would have one line with the 3 backticks, your code, and then one more line with just the 3 backticks. That allows this forum software to format your code nicely:

# The line above this is just ```
def function(parm):
    return parm
# The line after this is just ```

Please edit your post to put the backticks before and after the code - the code is virtually impossible to read as-is.

Also, I don’t see the code where you’re trying to actually calculate the sum, nor did you specify what the problem is that you’re having - please provide the specific error message or behavior you are seeing when your code is run.

Ken