Ben Kilah
Ben Kilah

Reputation: 3475

Django Integer and Decimal field multiplication

I'm trying to figure out how to multiply a decimal field value by an integer field value.

Here is my model:

class OrderItem(models.Model):
    order = models.ForeignKey(Order, editable=False)
    product = models.ForeignKey('products.Product')
    qty = models.IntegerField()
    original_price = models.DecimalField(max_digits=20, decimal_places=2)
    sale_price = models.DecimalField(max_digits=20, decimal_places=2)
    tax = models.DecimalField(max_digits=20, decimal_places=2)
    total = models.DecimalField(max_digits=2, decimal_places=2, null=True, blank=True, editable=False)
    total_tax = models.DecimalField(max_digits=2, decimal_places=2, null=True, blank=True, editable=False)
    picked = models.IntegerField(null=True, blank=True)
    user = models.ForeignKey('auth.User', editable=False)
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)

I'm trying to multiple the "sale_price" column by the "qty" column but get the following error:

Invalid literal for Decimal: u'180.00180.00'

Using this code:

def orderItemPs(sender, instance=False, **kwargs):
    #figure out the total
    instance.total = decimal.Decimal(int(instance.qty) * instance.sale_price)
    instance.total_tax = decimal.Decimal(int(instance.qty) * instance.tax)
    #disconnect
    post_save.disconnect(orderItemPs, sender=OrderItem)
    instance.save()
    #reconnect
    post_save.connect(orderItemPs, sender=OrderItem)

I'm not sure what I'm doing wrong.

Cheers, Sn0rcha!

UPDATE: Further information.

I've changed the code that was triggering off the post_save function to the following.

order_item = OrderItem(order=form, product=Product.objects.get(pk=products[i]), qty=int(qtys[i]), original_price=decimal.Decimal(original_prices[i]), tax=decimal.Decimal(taxs[i]), user=request.user, sale_price=decimal.Decimal(sale_prices[i]))

to make sure I'm casting the form values correctly prior to saving the model. I changed the 2 lines in the post save function to:

instance.total = decimal.Decimal(instance.qty * instance.sale_price)
instance.total_tax = decimal.Decimal(instance.qty * instance.tax)

However now I get the error:

quantize result has too many digits for current context

I'm using POSTGres not that it's an error on it's end.

Upvotes: 0

Views: 6721

Answers (1)

Karmel
Karmel

Reputation: 3472

I'm guessing int(instance.qty) is 2, and instance.sale_price is '180.00'-- a string, not a numeric value, which, when multiplied by two, just duplicates the string:

>>> decimal.Decimal(2 * '180.00')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/EPD64.framework/Versions/7.2/lib/python2.7/decimal.py", line 548, in __new__
    "Invalid literal for Decimal: %r" % value)
  File "/Library/Frameworks/EPD64.framework/Versions/7.2/lib/python2.7/decimal.py", line 3844, in _raise_error
    raise error(explanation)
decimal.InvalidOperation: Invalid literal for Decimal: '180.00180.00'

How is sale_price being set? I would hope that Django casts it as a Decimal, but is it possible it's being stored as a string somehow?

Upvotes: 1

Related Questions