1

I have theses model and admin model as displayed beloy

I want in the model for transaction to display total amount ( thats is the sum of amount field)

There is not method for change list in Inline how do I do this

class TransactionAdmin(admin.ModelAdmin):
    inlines = [TransactionAmountInline, AmountPaidInline, PhotosInline]
    fields = ('customer', 'title', 'description', 'created_at')
    readonly_fields = ('updated_at',)
    list_display = ('title', 'customer')


class AmountPaidInline(admin.TabularInline):
   model = AmountPaid
   extra = 0


class AmountPaid(models.Model):
     transaction = models.ForeignKey(Transactions)
     description = models.TextField(null=True)
     amount = models.DecimalField(max_digits=19, decimal_places=4)
     created_at = models.DateTimeField(auto_now_add=True)
     updated_at = models.DateTimeField(auto_now=True)

1 Answer 1

1

You have to add this function to your Transactions model.

def sum_amount (self):
    return AmountPaid.objects.filter(transaction=self).aggregate(total=Sum('amount'))['total']

And add the function name to the list_display option of TransactionAdmin, the model admin:

list_display = ('title', 'customer', 'sum_amount')
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for your answer actually what I what I want is to display the sum_amount in the change form( when you click on the transaction link) I know I can override the change list view to and add the total amount to the extra context but I want to display the total amount in the AmountPaidInline form that its going to be displayed
In simple I want to add another row to the inline to display the total amount in the AmountPaind model
please give me more help in this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.