对于钱包应用 我为django.contrib.auth.models用户模型的每个用户创建了一个配置文件。当用户创建帐户时,他们的个人资料被创建并保存。但是,当我尝试添加钱时,我得到一个名为Userprofile的错误,不存在用户没有配置文件。所以我检查了数据库,并且他们的Userprofile存在正确的用户。我真的很困惑为什么会这样。
// add_money view
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 view
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)
// Userprofile模型
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,显示了管理员的用户名,这在我退出时解决了。谢谢你的帮助**
答案 0 :(得分:1)
此行发生错误
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