Django比特币支付模块十进制JSON可序列化错误

问题描述 投票:0回答:1

我正在开发一个python django项目,用户将比特币发送到外部地址。这就是支付模块的样子。我已经尝试了许多建议,但没有提供任何帮助。

以下是我的代码。我很感激能帮我解决这个错误的第三只眼睛。

import decimal as Decimal
import bitcoinrpc

class Withdrawal(models.Model):
    amount = models.OneToOneField("Amount", null=True)
    payment = models.OneToOneField(Payment, null=True, blank=True)
    to = models.CharField(max_length=35, null=True, blank=True)
    account = models.ForeignKey(Account)
    complete = models.BooleanField(default=False)
    txid = models.CharField(max_length=64, blank=True, null=True)
def __str__(self):
    return u"{} from {} to {}".format(self.amount, self.account, self.to)

def alert(self):
    """Sends email to all staff users about failed withdrawal"""
    for u in User.objects.filter(is_superuser=True):
        if u.email != u'':
            message = "Withdrawal {}, by {}, for {} " \
                      "has failed due to an insufficient balance."
            message = message.format(self.pk,
                                     self.account.user.email,
                                     self.amount)
            send_mail('FAILED WITHDRAWAL',
                      message,
                      settings.DEFAULT_FROM_EMAIL,
                      [u.email],
                      fail_silently=False)
    raise Exception("Insufficient funds")  

def send(self):
    """Sends payment on the blockchain, or if unable calls self.alert"""
    if self.complete:
        raise Exception("Sorry, Can't send a completed payment")
    s = get_server()

    fee = Decimal(str(PAYMENTS_FEE))
    final_amount = self.amount.base_amt - fee
    inputs = s.proxy.listunspent(0,
                                 9999999)

    total = sum([t['amount'] for t in inputs])
    if total  0:
        outputs[get_change()] = Decimal(total) -final_amount - fee
    raw = s.createrawtransaction(inputs, outputs)

    s.walletpassphrase(PAYMENTS_PASSPHRASE, 10)
    signed = s.signrawtransaction(raw)
    sent = s.proxy.sendrawtransaction(signed['hex'])
    s.walletlock()

    self.complete = True
    self.txid = sent
    self.save()
    if self.payment:
        self.payment.txid = sent
        self.confirmed = True
        self.payment.save()

    print sent


@staticmethod
def process_payments():
    """Try to send all present payments"""
    for w in Withdrawal.objects.filter(complete=False):
        w.send()

执行此代码时,会发生错误。这是追溯

Traceback (most recent call last):
  File "/home/mart/forexapp/manage.py", line 10, in 
    execute_from_command_line(sys.argv)
  File "/home/mart/forexapp/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 351, in execute_from_command_line
    utility.execute()
  File "/home/mart/forexapp/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 343, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/mart/forexapp/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 394, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/mart/forexapp/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 445, in execute
    output = self.handle(*args, **options)
  File "/home/mart/forexapp/payments/management/commands/process_payments.py", line 6, in handle
    Withdrawal.process_payments()
  File "/home/mart/forexapp/payments/models.py", line 35, in process_payments
    w.send()
  File "/home/mart/forexapp/payments/models.py", line 23, in send
    raw = s.createrawtransaction(inputs, outputs)
  File "/home/mart/forexapp/venv/local/lib/python2.7/site-packages/bitcoinrpc/connection.py", line 357, in createrawtransaction
    return self.proxy.createrawtransaction(inputs, outputs)
  File "/home/mart/forexapp/venv/local/lib/python2.7/site-packages/bitcoinrpc/proxy.py", line 91, in __call__
    'id': self.__idcnt})
  File "/usr/lib/python2.7/json/__init__.py", line 244, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python2.7/json/encoder.py", line 207, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python2.7/json/encoder.py", line 270, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python2.7/json/encoder.py", line 184, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: Decimal('0.0016512') is not JSON serializable

我在这里做错了什么?

django python-2.7 decimal bitcoin
1个回答
0
投票

Decimal是一个对象,JSON正在寻找一个数字。需要将小数转换为浮点数。

© www.soinside.com 2019 - 2024. All rights reserved.