Django视图没有保存到模型

时间:2018-04-11 21:31:47

标签: python django

我有一个视图,当用户点击网站上的按钮时,我会调用它,它会提取JSON数据(来自Coinbase API)并将其转换为字符串,并且将拉出的字符串保存到当前用户&# 39;帐户

每当他们点击按钮时,它都会拉出字符串,但没有任何内容保存到帐户中,这就是问题所在。

views.py

{
    "rules": [{
            "precedence": 0,
            "conditions": [{
                    "field": "Name",
                    "relation": "is not one of",
                    "value": "Mary"
                }
            ]
        }
    ]
}

models.py

from django.shortcuts import render
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required
from ico_login.models import UserAddress 
from coinbase.wallet.client import Client


@login_required()
def generate_address_btc(request, *args, **kwargs):
    client = Client('api', 'key')
    r = client.get_addresses('account_id')
    address = r['data'][0]['address']
    request.user.address = str(address)
    request.user.save()

    return HttpResponse(address)

urls.py

from django.db import models
from django.contrib.auth.models import User


class UserAddress(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    address = models.CharField(max_length=300, default=' ')

1 个答案:

答案 0 :(得分:3)

在您的视图中,您编写2X Mar 201X 15:36:36,880 DEBUG [ambari-client-thread-37] FilterBasedLdapUserSearch:115 - Searching for user 'XXXX', with user search [ searchFilter: '(&(sAMAccountName={0})(objectClass=posixAccount))', searchBase: '', scope: subtree, searchTimeLimit: 0, derefLinkFlag: false ] 2X Mar 201X 15:36:36,917 DEBUG [ambari-client-thread-37] AbstractContextSource:349 - Got Ldap context on server 'ldap://rXXX92.corp.XXX.com:389/DC=corp,DC=XXX,DC=com' 2X Mar 201X 15:36:36,929 DEBUG [ambari-client-thread-37] DefaultAuthenticationEventPublisher:94 - No event was found for the exception org.apache.ambari.server.security.authorization.InvalidUsernamePasswordCombinationException 2X Mar 201X 15:36:36,929 DEBUG [ambari-client-thread-37] AmbariBasicAuthenticationFilter:185 - Authentication request for failed: org.apache.ambari.server.security.authorization.InvalidUsernamePasswordCombinationException: Unable to sign in. Invalid username/password combination. ,因此您将属性request.user.address = ...添加到address对象。至于我读你的代码,这不是你想要的。您想将User放入与str(address) address对象相关的对象UserAddress的字段User中,对吧?!

所以,这是建议的编辑:

models.py

request.user

views.py

from django.db import models
from django.contrib.auth.models import User


class UserAddress(models.Model):
    # next line changed
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='user_address')
    address = models.CharField(max_length=300, default=' ')

希望有帮助和快乐的编码!