karnataka
karnataka

Reputation: 445

django - DecimalField act as float field with default value 0.00?

i have code below

CartModel

total = models.DecimalField(default=0.00, max_digits=10, decimal_places=2)

for this total, i checked in admin its value is 0.0 and type(total) showing as Float instead of Decimal and if assign value Decimal('13.00') it show error like

TypeError: unsupported operand type(s) for +=: 'float' and 'decimal.Decimal'

why default=0.00 consider as float and giving this error?

i am debugging below code

Media model
    price = models.DecimalField(default=0.00, max_digits=10, decimal_places=2, blank=True, null=True)


@receiver(post_save, sender=CartEntry)
def update_media_cart_on_create(sender, instance, **kwargs):
    line_cost = instance.media.price
    instance.cart.total += line_cost
    instance.cart.count += 1
    instance.cart.updated = timezone.now()
    instance.cart.save()

enter image description here

Upvotes: 2

Views: 1246

Answers (1)

souldeux
souldeux

Reputation: 3755

You're providing a float as the default value. Instead, provide a Decimal default: Decimal('0.00').

If you haven't already, you'll need to from decimal import Decimal.

Upvotes: 4

Related Questions