0

I am working to learn Django, and have built a test database to work with. I have a table that provides basic vendor invoice information, so, and I want to simply present a user with the total value of invoices that have been loaded to into the database. I found that the following queryset does sum the column as I'd hoped:

total_outstanding: object = Invoice.objects.aggregate(Sum('invoice_amount'))

but the result is presented on the page in the following unhelpful way:

Total $ Outstanding: {'invoice_amount__sum': Decimal('1965')}

The 1965 is the correct total for the invoices that I populated the database with, so the queryset is pulling what I want it to, but I just want to present that portion of the result to the user, without the other stuff.

Someone else asked a similar question (basically the same) here: how-to-extract-data-from-django-queryset, but the answer makes no sense to me, it is just:

k = k[0] = {'name': 'John'}

Queryset is list .

Can anyone help me with a plain-English explanation of how I can extract just the numerical result of that query for presentation to a user?

1 Answer 1

2

What you here get is a dictionary that maps the name of the aggregate to the corresponding value. You can use subscripting to obtain the corresponding value:

object = Invoice.objects.aggregate(
    Sum('in,voice_amount')
)['invoice_amount__sum']
Sign up to request clarification or add additional context in comments.

Comments

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.