Aggregation on json field float value

@leandrodesouzadev’s answer should work but you can pass output_field directly to Sum with the same results.

Example.objects.aggregate(
    Sum("data__float", output_field=FloatField())
)

What’s happening here is that since JSONField are schemaless the ORM has no way to know that data__float is actually a JSON float. You can to tell it is the case otherwise it will try to json.loads the data returned from the database.

2 Likes