用户资料不存在用户没有资料
Userprofile does not exist user has no profile
对于钱包应用
我为 django.contrib.auth.models 用户模型的每个用户创建了一个配置文件。当用户创建帐户时,会创建并保存他们的个人资料。但是,当我尝试充值时,我收到一条错误消息,称为 Userprofile does not exist user has no profile。所以我检查了数据库,并且存在具有正确用户的用户配置文件。我真的很困惑为什么会这样。
//add_money 查看
def add_money(request):
if request.user:
if request.user.has_perm('add_money'):
if request.POST and request.POST.get('amount'):
username = request.user.username
add_amount = request.POST.get('amount')
wallet = Wallet.objects.get(pk=request.user.userprofile.wallet_id_id)
wallet.add_money(add_amount)
wallet.save()
now = datetime.now()
trans = Transaction(from_name=username, wallet_id=wallet, date=now, amount=add_amount)
trans.save()
return render(request, 'user_profile.html', {'user': request.user,'userprofile': request.user.userprofile, 'wallet': wallet})
else:
return render(request, 'add_money.html')
else:
return render(request, 'access_denied.html')
else:
return HttpResponseRedirect('/login/?next={}'.format('/add_money/'))
//create_user 查看
def create_user(request):
form = UserReg(request.POST or None)
if form.is_valid():
user = form.save(commit=False)
username = form.cleaned_data['username']
password = form.cleaned_data['password']
user.set_password(password)
wallet = Wallet(username=request.POST['username'], amount=0)
wallet.save()
user.save()
userprofile = Userprofile(user=user, wallet_id=wallet)
userprofile.save()
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
return render(request, 'user_profile.html', {'user': user, 'wallet': wallet})
context = {
"form": form,
}
return render(request, 'create_user.html',context)
//用户档案模型
class Userprofile(models.Model):
user = models.OneToOneField(User, unique=True, primary_key=True)
wallet_id = models.ForeignKey(Wallet, on_delete=models.CASCADE)
date_ob = models.DateField(null=True, blank=True)
sex = models.CharField(max_length=1, null=True)
def __str__(self):
return self.user
数据库图片
回溯:
File "/Users/ravinkohli/env_app_pw/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/ravinkohli/PycharmProjects/untitled1/wallet/views.py" in add_money
19. wallet = Wallet.objects.get(pk=request.user.userprofile.wallet_id_id)
File "/Users/ravinkohli/env_app_pw/lib/python2.7/site-packages/django/utils/functional.py" in inner
226. return func(self._wrapped, *args)
File "/Users/ravinkohli/env_app_pw/lib/python2.7/site-packages/django/db/models/fields/related.py" in __get__
480. self.related.get_accessor_name()
Exception Type: RelatedObjectDoesNotExist at /add_money/
Exception Value: User has no userprofile.
**编辑:
当我在另一个选项卡上退出管理站点时,问题就解决了,我打印了 request.user.username 并且显示了管理员的用户名,这在我注销时解决了。感谢您的帮助**
这个问题的解决方案。
错误发生在这一行
wallet = Wallet.objects.get(pk=request.user.userprofile.wallet_id_id)
显然是因为用户的用户配置文件不存在。这可能有两个主要原因。
- 用户是匿名用户(未验证)您没有检查用户是否已登录
- 用户帐户是在您创建用户个人资料之前创建的。
先检查配置文件是否存在
if not getattr(request.user,'userprofile'):
# create user profile here
wallet = Wallet.objects.get(pk=request.user.userprofile.wallet_id_id)
真正的解决方案
你的数据库设计有误。看到这个。
wallet = Wallet(username=request.POST['username'], amount=0)
这意味着您的钱包模型中有一个用户名字段,但是
class Userprofile(models.Model):
user = models.OneToOneField(User, unique=True, primary_key=True)
wallet_id = models.ForeignKey(Wallet, on_delete=models.CASCADE)
你有一个钱包作为 UserProfile 的外键,这导致了循环关系。
正确的设计是从您的电子钱包中删除用户名字段并用用户字段替换它
class Wallet(models.Model):
user = models.ForeignKey(User)
...
那么你的用户资料就变成了
class Userprofile(models.Model):
user = models.OneToOneField(User, unique=True, primary_key=True)
date_ob = models.DateField(null=True, blank=True)
sex = models.CharField(max_length=1, null=True)
def __str__(self):
return self.user
对于钱包应用 我为 django.contrib.auth.models 用户模型的每个用户创建了一个配置文件。当用户创建帐户时,会创建并保存他们的个人资料。但是,当我尝试充值时,我收到一条错误消息,称为 Userprofile does not exist user has no profile。所以我检查了数据库,并且存在具有正确用户的用户配置文件。我真的很困惑为什么会这样。
//add_money 查看
def add_money(request):
if request.user:
if request.user.has_perm('add_money'):
if request.POST and request.POST.get('amount'):
username = request.user.username
add_amount = request.POST.get('amount')
wallet = Wallet.objects.get(pk=request.user.userprofile.wallet_id_id)
wallet.add_money(add_amount)
wallet.save()
now = datetime.now()
trans = Transaction(from_name=username, wallet_id=wallet, date=now, amount=add_amount)
trans.save()
return render(request, 'user_profile.html', {'user': request.user,'userprofile': request.user.userprofile, 'wallet': wallet})
else:
return render(request, 'add_money.html')
else:
return render(request, 'access_denied.html')
else:
return HttpResponseRedirect('/login/?next={}'.format('/add_money/'))
//create_user 查看
def create_user(request):
form = UserReg(request.POST or None)
if form.is_valid():
user = form.save(commit=False)
username = form.cleaned_data['username']
password = form.cleaned_data['password']
user.set_password(password)
wallet = Wallet(username=request.POST['username'], amount=0)
wallet.save()
user.save()
userprofile = Userprofile(user=user, wallet_id=wallet)
userprofile.save()
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
return render(request, 'user_profile.html', {'user': user, 'wallet': wallet})
context = {
"form": form,
}
return render(request, 'create_user.html',context)
//用户档案模型
class Userprofile(models.Model):
user = models.OneToOneField(User, unique=True, primary_key=True)
wallet_id = models.ForeignKey(Wallet, on_delete=models.CASCADE)
date_ob = models.DateField(null=True, blank=True)
sex = models.CharField(max_length=1, null=True)
def __str__(self):
return self.user
数据库图片
File "/Users/ravinkohli/env_app_pw/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/ravinkohli/PycharmProjects/untitled1/wallet/views.py" in add_money
19. wallet = Wallet.objects.get(pk=request.user.userprofile.wallet_id_id)
File "/Users/ravinkohli/env_app_pw/lib/python2.7/site-packages/django/utils/functional.py" in inner
226. return func(self._wrapped, *args)
File "/Users/ravinkohli/env_app_pw/lib/python2.7/site-packages/django/db/models/fields/related.py" in __get__
480. self.related.get_accessor_name()
Exception Type: RelatedObjectDoesNotExist at /add_money/
Exception Value: User has no userprofile.
**编辑:
当我在另一个选项卡上退出管理站点时,问题就解决了,我打印了 request.user.username 并且显示了管理员的用户名,这在我注销时解决了。感谢您的帮助**
这个问题的解决方案。
错误发生在这一行
wallet = Wallet.objects.get(pk=request.user.userprofile.wallet_id_id)
显然是因为用户的用户配置文件不存在。这可能有两个主要原因。
- 用户是匿名用户(未验证)您没有检查用户是否已登录
- 用户帐户是在您创建用户个人资料之前创建的。
先检查配置文件是否存在
if not getattr(request.user,'userprofile'):
# create user profile here
wallet = Wallet.objects.get(pk=request.user.userprofile.wallet_id_id)
真正的解决方案
你的数据库设计有误。看到这个。
wallet = Wallet(username=request.POST['username'], amount=0)
这意味着您的钱包模型中有一个用户名字段,但是
class Userprofile(models.Model):
user = models.OneToOneField(User, unique=True, primary_key=True)
wallet_id = models.ForeignKey(Wallet, on_delete=models.CASCADE)
你有一个钱包作为 UserProfile 的外键,这导致了循环关系。
正确的设计是从您的电子钱包中删除用户名字段并用用户字段替换它
class Wallet(models.Model):
user = models.ForeignKey(User)
...
那么你的用户资料就变成了
class Userprofile(models.Model):
user = models.OneToOneField(User, unique=True, primary_key=True)
date_ob = models.DateField(null=True, blank=True)
sex = models.CharField(max_length=1, null=True)
def __str__(self):
return self.user