0

addon_income = round(pendingAmount*0.1, 2)
print(addon_income)  # if pendingAmount = 6, addon_income = 0.6 which is ok here

Wallet.objects.filter(id=###).update(
    active=F('active')+addon_income, total=F('total')+addon_income,
    uddate_time=timezone.now()
)

In the queryset above, if the F('active') = 41.2, F('total') = 41.2, and addon_income = 0.6, the active and total becomes 41.800000000000004 and 41.800000000000004 after the updating.

I tried to use round() in the queryset as shown below:

Wallet.objects.filter(id=###).update(
    active=round(F('active')+addon_income, 2), total=round(F('total')+addon_income, 2),
    uddate_time=timezone.now()
)

but it returns error: type CombinedExpression doesn't define round method

Anyone has any suggestion? Thx!

mrhaoji
  • 336
  • 5
  • 19

2 Answers2

1

I tried the solution in this post: Django ORM how to Round an Avg result

class Round(Func):
  function = 'ROUND'
  arity = 2

Book.objects.all().aggregate(Round(Avg('price'), 2))
mrhaoji
  • 336
  • 5
  • 19
0

Django has a Cast function that use can use in queries. In that function, you can specify FloatField(decimal_places=2)

blue_note
  • 27,712
  • 9
  • 72
  • 90
  • 1
    Django model `FloatField` has no argument named `decimal_points` – JPG Apr 24 '19 at 09:23
  • @JPG: yep, meant `places` – blue_note Apr 24 '19 at 09:25
  • `FloatField` has neither `decimal_places` nor `places` parameter. The Django documentation doesn't have anything like that for that field, in any of the available versions: https://docs.djangoproject.com/en/4.0/ref/models/fields/#django.db.models.FloatField – Person Dec 21 '21 at 09:01