我试图从用户模型中捕获用户,然后将该信息用作外键以将数据保存到另一个模型。注册表单会将用户信息保存到用户模型和其他UserProfile模型中。然后,该数据用于查询返回帐户ID号的API。然后,该ID号将存储在另一个通过外键链接到User模型的数据库中。
我尝试了很多可能性,但是没有用。
class registeraccount(TemplateView):
def get(self, request):
form = ExtendedUserCreationForm()
profile_form = UserProfileForm()
post1 = User.objects.all()
post2 = UserProfile.objects.all()
return render(request, 'main/register.html', context={'form': form, 'profile_form': profile_form})
def post(self, request):
if request.method == "POST":
form = ExtendedUserCreationForm(request.POST)
profile_form = UserProfileForm(request.POST)
if form.is_valid() and profile_form.is_valid():
user = form.save()
user.save()
profile = profile_form.save(commit=False)
profile.user = user
profile.save()
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password1')
user = authenticate(username=username, password=password)
login(request, user)
obj = User.objects.order_by("-id")[0]
a = obj
print(a.username)
obj2 = UserProfile.objects.order_by("-id")[0]
b = obj2
print(b.street)
try:
login_id = 'ap133875@gmail.com'
api_key = '##############################'
environment = currencycloud.Config.ENV_DEMO
client = currencycloud.Client(login_id, api_key, environment)
account = client.accounts.create(account_name=(a.username), legal_entity_type=(b.legal_entity_type), street=(b.street), city=(b.city), postal_code=(b.postal_code), country=(b.country))
print("Your account has been created")
account = client.accounts.find()
c = account
d = (c[-1].id)
print(d)
my_p = User.objects.get(request.user)
post = Currencyclouds(CC_account_id=d, user=my_user)
post.save()
return render(request, 'main/account_created.html', {'content': ['Your account is created']})
except ApiError as e:
print("Conversion encountered an error: {0} (HTTP code {1})".format(e.code, e.status_code))
print(p)
else:
form = ExtendedUserCreationForm()
profile_form = UserProfileForm()
return render(request, 'main/account_created.html', {'content': "Your account is created"})