views:

34

answers:

1

models.py

class Head(models.Model):
    total_amount=models.DecimalField(max_digits=9,decimal_places=2)

class Items(models.Model):
    head = models.ForeignKey(Head)
    item_amount=models.DecimalField(max_digits=9,decimal_places=2)

admin.py

class ItemsInline(admin.TabularInline):
    model = Items

class HeadAdmin(admin.ModelAdmin:
    inlines = [ItemsInline]

admin.site.register(Head,HeadAdmin)

Question

After fill in 3 items inline of HeadAdmin, how to sum all of item amounts and place to total_amount in Head after click save?

+1  A: 

Why do you want to do this? It's probably better to calculate the amount when you need it, via the annotation methods:

from django.db import Sum
my_head = Head.objects.get(pk=myid).annotate(item_sum=Sum(item__itemamount))
Daniel Roseman