Closed

Fix issue with DecimalField for Django

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/[url removed, login to view]", line 170, in create

[url removed, login to view]()

File "/home/payment/payment/lib/python2.6/site-packages/django/db/models/[url removed, login to view]", 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/[url removed, login to view]", line 483, in save_base

values = [(f, f.get_db_prep_save(raw and getattr(self, [url removed, login to view]) 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/[url removed, login to view]", 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/[url removed, login to view]", line 598, in to_python

return [url removed, login to view](value)

File "/usr/lib/python2.6/[url removed, login to view]", 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

#[url removed, login to view] = str([url removed, login to view]['subscription_price'])

[url removed, login to view] = Decimal("%.2f" % (float([url removed, login to view]['subscription_price'])))

log('Decimal price: %s' % [url removed, login to view])

if [url removed, login to view] == Decimal("0.00"):

[url removed, login to view] = None

log('Decimal price = None')

Models are as follows (I only pasted the relevant DecimalField fields):

class Subscription([url removed, login to view]):

# standard month to month, year to year, etc. pricing.

price = [url removed, login to view](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 = [url removed, login to view](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 = [url removed, login to view](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

Skills: Django, Python

See more: django decimalfield, django decimal, decimalfield convert float decimal, convert float decimal convert float string, decimalfield django, home base data, django decimalfields, django convert float decimal convert float string, django decimalfield default value, django decimalfield default, django decimalfield blank, django decimalfield string float, issue payment, db subscription code, data str, c standard lib, auto programmers, python for c programmers, python and django, str, Python/Django, python django, python auto, for f, fix api

About the Employer:
( 9 reviews ) Brooklyn, United States

Project ID: #4225135

7 freelancers are bidding on average $67 for this job

rmrf

Hi, please check your inbox. Thanks.

$30 USD in 1 day
(8 Reviews)
3.4
abanipatra

Hi, I would like to fix your issue.

$30 USD in 1 day
(4 Reviews)
3.2
dmonroy

Hi, I can solve your issue.

$30 USD in 0 days
(0 Reviews)
3.5
mkagenius

You there?

$30 USD in 1 day
(0 Reviews)
0.0
haykayltduk

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 More

$250 USD in 1 day
(0 Reviews)
0.0
shoaibanwar

hi, let me try this and check you PMB please

$50 USD in 1 day
(0 Reviews)
0.0
comtom

Hi, I'm a Django and python expert. I can help you with this project. For further information, send me a message.

$50 USD in 1 day
(0 Reviews)
0.0