Django - 保存前设置一个值

时间:2017-05-22 01:53:38

标签: python django django-models

我正在尝试在用户注册时创建一个钱包,但是当用户填写表单时,他没有被重定向到家中,因此很少在银行中存在并出现错误

有人可以帮我修复此错误吗?

Models.py
===============================================

from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
from coinbase.wallet.client import Client

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    location = models.CharField(max_length=30, blank=True)
    birth_date = models.DateField(null=True, blank=True)
    key_simple = models.CharField(max_length=128, blank=True)
    key_secret = models.CharField(max_length=800, blank=True)
    wallet = models.CharField(max_length=30, blank=True)

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        client = Client('xxxxxxxxxxxxxxxxx', 'xxxxxxxxxxxxxx')
        data_account = client.create_account(name= 'Wallet of %s' % user.name)
        profile.walletid = data_account['id']
        data_address = client.create_address(data_account['id'])

        wallet_address_id = data_address['id']
        profile.wallet = data_address['address']
        Profile.objects.create(user=instance)

@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    instance.profile.save()

0 个答案:

没有答案