Django2 - 自动为用户注册分配一定数量的加密货币

时间:2018-05-10 14:59:20

标签: python django model

早上好,

我们目前正在开展一个学校项目,我们必须建立自己的加密货币市场。到目前为止一切都很好。

现在,当用户在我们的网站上注册/注册时,他会自动获得一定数量的特定加密货币。这被视为银行与创建用户之间的交易。

我们有自定义注册表单,并且相信我们需要在此处实现此功能所需的代码,但不知道如何完成此操作。

非常感谢您的帮助

我们在views.py中的注册表单

#signup form
def signup(request):
    if request.method =='POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            form.save()
            #We want to log in the user after his sign up
            username = form.cleaned_data.get('username')
            raw_password = form.cleaned_data.get('password1')
            user = authenticate(username=username, password=raw_password)
            login(request, user)
            return redirect('/')
    else:
        form = SignUpForm()
    return render(request, 'sign-up.html', {'SignUpForm': form})

我们的交易模式。我们使用django用户模型

class Transaction(models.Model):
    listing_id = models.ForeignKey(Listing, on_delete=models.CASCADE, null=True, blank=True)
    created_at = models.DateTimeField(auto_now_add = True)
    exchange_amount = models.IntegerField(null=True)
    user_debit = models.ForeignKey(User, on_delete=models.CASCADE, related_name='user_debit', null=True)
    user_credit = models.ForeignKey(User, on_delete=models.CASCADE, related_name='user_credit', null=True)
    currency_id = models.ForeignKey(Currency, on_delete=models.CASCADE, null=True)

1 个答案:

答案 0 :(得分:0)

只需在注册期间创建一个交易。我假设银行也是(货币依赖?)用户,比如id = 1。然后就像,

@NonNull

会奏效。但是,您不必在视图中执行此操作。例如,您可以使用post save signals

相关问题