本文介绍了一个对象不可写意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不知道这是怎么回事...我只想检查模型字段的值,然后相应地对其进行更新...任何帮助或见识都受到赞赏!
I don't know what's going on here... I just want to check the value of a model field and then update it accordingly... any help or insight is appreciated!
模型:
class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True)
beta = models.CharField(max_length=1, blank=True, null=True)
视图:
from internal.accounts.models import UserProfile
from django.contrib.auth.models import User
@login_required
def beta_testers(request):
user = User.objects.get(username=request.user.username)
user_profile = user.get_profile()
count = UserProfile.objects.filter(beta='1').count()
if count < 50 and not user_profile['beta']:
user_profile['beta'] = '1'
user_profile.save()
错误:
TypeError at /utilities/beta-signup/
'UserProfile' object is unsubscriptable
Request Method: GET
Request URL: http://localhost/utilities/beta-signup/?x=1&y=15
Django Version: 1.2.1
Exception Type: TypeError
Exception Value:
'UserProfile' object is unsubscriptable
Exception Location: C:/django\internal\cms_helper\views.py in beta_testers, line 284
推荐答案
错误为无法订阅。您的user_profile对象不是字典。使用 user_profile.beta
,而不是 user_profile ['beta']
。
The error is "unSUBscriptable". Your user_profile object isn't a dictionary. Use user_profile.beta
, not user_profile['beta']
.
这篇关于一个对象不可写意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!