Suljettu

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

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("[url removed, login to view]"):

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

Taidot: Django, Python

Näytä lisää: django decimalfield, django decimal, decimalfield convert float decimal, decimalfield django, home base data, django decimalfields, django decimalfield default value, django decimalfield default, django decimalfield blank, django decimalfield string float, issue payment, data str, standard lib, auto programmers, str, python django, python auto, for f, fix api, django, access django, web api python, typeerror, raw object, string float

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

Projektin tunnus: #4225135

7 freelanceria on tarjonnut keskimäärin 67 $ tähän työhön

rmrf

Hi, please check your inbox. Thanks.

30 $ USD 1 päivässä
(8 arvostelua)
3.4
abanipatra

Hi, I would like to fix your issue.

30 $ USD 1 päivässä
(4 arvostelua)
3.2
dmonroy

Hi, I can solve your issue.

30 $ USD 0 päivässä
(0 arvostelua)
3.5
mkagenius

You there?

30 $ USD 1 päivässä
(0 arvostelua)
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 Lisää

250 $ USD 1 päivässä
(0 arvostelua)
0.0
shoaibanwar

hi, let me try this and check you PMB please

50 $ USD 1 päivässä
(0 arvostelua)
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 1 päivässä
(0 arvostelua)
0.0