B Gonza
B Gonza

Reputation: 25

Trying to use sum value django

How can I use the result of this sum in a queryset?

query = some_model.objects.aggregate(Sum('some_field'))

I already tried this

regPeso = test_model.objects.create(test_field_id =1,total_field = query)

I got this error

Field 'total_field' expected a number but got "{'some_field__sum': x}"

What can i do to solve this error?, Thanks you!

Upvotes: 1

Views: 40

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476574

You obtain the value from the dictionary for a given key by subscripting, so:

regPeso = test_model.objects.create(
    test_field_id=1,
    total_field=query['some_field__sum']
)

Upvotes: 1

Related Questions