我想在用户每次填写一张表格时给用户十枚硬币
我想在用户每次填写一个表格时给用户十枚硬币,所以我尝试了此代码并显示此
TypeError at /profile
unsupported operand type(s) for +=: 'DeferredAttribute' and 'int'
error in views.py in line += 10
模型
class User(AbstractUser):
user_pic = models.ImageField(upload_to='img/',default="",null=True, blank=True)
coins = models.IntegerField(default=100)
def gives_user_coins_after_create(sender, instance, created, **kwargs):
if created:
profile = instance.User.profile
profile.coins += 10
profile.save()
。
@login_required(login_url='accounts/login/')
def profile(request):
# Gives the user 10 coins for fill up one form
profile = get_user_model()
profile.coins += 10
profile.save()
return render(request,'account/profile.html')
错误
I want to give users ten coins each time they fill out one form , so i tried this code above and show this error
TypeError at /profile
unsupported operand type(s) for +=: 'DeferredAttribute' and 'int'
error in views.py in line += 10
models.py
class User(AbstractUser):
user_pic = models.ImageField(upload_to='img/',default="",null=True, blank=True)
coins = models.IntegerField(default=100)
def gives_user_coins_after_create(sender, instance, created, **kwargs):
if created:
profile = instance.User.profile
profile.coins += 10
profile.save()
views.py
@login_required(login_url='accounts/login/')
def profile(request):
# Gives the user 10 coins for fill up one form
profile = get_user_model()
profile.coins += 10
profile.save()
return render(request,'account/profile.html')
i tried many code but nothing work , any idea or help please
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您上面显示的
用户
模型是您的auth_user_model
,那么您真正需要的只是(使用
f()状况);没有
.profile
您需要访问。然后,您可以在您的
formView
'sform_valid
:If the
User
model you show above is yourAUTH_USER_MODEL
then all you really need is(which is using
F()
to avoid (some) race conditions); there is no.profile
you'd need to access.You can then call this in e.g. your
FormView
'sform_valid
: