python - Django Integer and Decimal field multiplication -


i'm trying figure out how multiply decimal field value integer field value.

here 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 multiple "sale_price" column "qty" column following error:

invalid literal decimal: u'180.00180.00' 

using code:

def orderitemps(sender, instance=false, **kwargs):     #figure out 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 i'm doing wrong.

cheers, sn0rcha!

update: further information.

i've changed code triggering off post_save function 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 form values correctly prior saving model. changed 2 lines in post save function to:

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

however error:

quantize result has many digits current context 

i'm using postgres not it's error on it's end.

i'm guessing int(instance.qty) 2, , instance.sale_price '180.00'-- string, not numeric value, which, when multiplied two, duplicates 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 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 decimal: '180.00180.00' 

how sale_price being set? hope django casts decimal, possible it's being stored string somehow?


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -