percentage with django views

I have this statement: received = Shipments.objects.aggregate(Sum(“received”)) which returns [4869]. I would like to get the value without the . I would just like to get the value so I can work on doing percentage, I looked all over the web but could not find my solution. Thank you

Please post the complete view in which this code is executing, along with what you’re looking at as the output. It would also be helpful if you identified what your end objective is.

You might want to review the docs and examples at Aggregation | Django documentation | Django to remind yourself what exactly an aggregate will return.

this is my view:

def stats(request):
    received = Shipments.objects.aggregate(Sum("received"))
    bad = Shipments.objects.aggregate(Sum("bad"))
    non = Shipments.objects.aggregate(Sum("non"))
    doa = Shipments.objects.aggregate(Sum("doa"))
    para = Shipments.objects.aggregate(Sum("para"))
    released = Shipments.objects.aggregate(Sum("released"))

    context = {
        "received": list(received.values()),
        "bad": list(received.values()),
        "non": list(bad.values()),
        "doa": list(non.values()),
        "para": list(para.values()),
        "released": list(released.values()),
    }

    return render(request, "shipments/show_stats.html", context)

what I am trying to do is get the percentage of the number of bad with the number received.

received = Shipments.objects.aggregate(Sum("received")) returns {'received__sum': 4869}

doa = Shipments.objects.aggregate(Sum("doa")) returns
{'para__sum': 148}

"received": list(received.values()), returns[4869]

"doa": list(non.values()), returns [371]

what I would like to do is get the value without the brackets. Then get the percentage of doa against the total receive

How do you access values from either a list or a dict?

With received = {‘received__sum’: 4869}, received is a dict. How do you access the value?

With doa = [371], doa is a list. How do you access any value from a list?

This is regular Python at this point - this isn’t anything special or “Django-ish”.

(If you’re not comfortable with Python data types, you might want to review 3. An Informal Introduction to Python — Python 3.12.5 documentation and 5. Data Structures — Python 3.12.5 documentation)

Side note: Instead of list(received.values()), see values_list with the flat=True parameter.

Side note 2: You can aggregate multiple values in a single query. You don’t need to do all these sums as separate statements.

Side note 3: You can also calculate the percentages within the query. See the price_diff example in the aggregation docs.

Side note 4: When posting blocks of code here, please remember to enclose the block between lines of three backtick - ` characters. This means you’ll have a line of ```, then the code, then another line of ```. If you want to mark off a single line or part of a line, use a single backtick ` before and after the code in the line. (All on the same line.) (I took the liberty of editing your post for clarity with this.)