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.