I'm getting the following error when trying to save a "Subscription" object via Django and DecimalFields:
02/08 09:28:37> Decimal price: 0.00
02/08 09:28:37> Decimal price = None
02/08 09:28:37> Traceback (most recent call last):
File "/home/payment/payment/web/api/[login to view URL]", line 170, in create
[login to view URL]()
File "/home/payment/payment/lib/python2.6/site-packages/django/db/models/[login to view URL]", line 410, in save
self.save_base(force_insert=force_insert, force_update=force_update)
File "/home/payment/payment/lib/python2.6/site-packages/django/db/models/[login to view URL]", line 483, in save_base
values = [(f, f.get_db_prep_save(raw and getattr(self, [login to view URL]) or f.pre_save(self, True))) for f in meta.local_fields if not isinstance(f, AutoField)]
File "/home/payment/payment/lib/python2.6/site-packages/django/db/models/fields/[login to view URL]", line 624, in get_db_prep_save
return connection.ops.value_to_db_decimal(self.to_python(value),
File "/home/payment/payment/lib/python2.6/site-packages/django/db/models/fields/[login to view URL]", line 598, in to_python
return [login to view URL](value)
File "/usr/lib/python2.6/[login to view URL]", line 649, in __new__
"First convert the float to a string")
TypeError: Cannot convert float to Decimal. First convert the float to a string
Here is the code:
# standard pricing
#[login to view URL] = str([login to view URL]['subscription_price'])
[login to view URL] = Decimal("%.2f" % (float([login to view URL]['subscription_price'])))
log('Decimal price: %s' % [login to view URL])
if [login to view URL] == Decimal("0.00"):
[login to view URL] = None
log('Decimal price = None')
Models are as follows (I only pasted the relevant DecimalField fields):
class Subscription([login to view URL]):
# standard month to month, year to year, etc. pricing.
price = [login to view URL](default=0.00, max_digits=10, decimal_places=2, null=True, blank=True) # Price of the item we auto-create every billing cycle (0 if None)
# price for extra users
price_per_extra_user = [login to view URL](default=0.00, max_digits=10, decimal_places=2, null=True, blank=True, help_text="The price for extra users.")
# first billing interval
first_price_per_num_dmy = [login to view URL](default=0.00, max_digits=10, decimal_places=2, null=True, blank=True, help_text="The first payment's price per user")
Please let me know if you know of a quick fix. I can provide SVN source code access... but most programmers shoul
Just do what it says:
str(float(...))
That is the quick fix.
For a proper fix you should be using django-money (djmoney) which basically handles all this for you -- I helped write it. And it also does currency stuff as well.