2011-01-06 11 views
2

私はここで何が起こっているのかわかりません...私はちょうどモデルフィールドの値をチェックし、それに応じてそれを更新したいと思います...任意のヘルプや洞察に感謝しています!オブジェクトが書かれていないとはどういう意味ですか?

モデル:

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 
+1

[Pythonでは、オブジェクトが添字付きかどうかはどういう意味ですか?](http://stackoverflow.com/questions/216972/in-python-what-does-it-mean-もしもオブジェクトが添え書きかどうか) – mipadi

答えて

7

エラーが "unSUBscriptable" です。あなたのuser_profileオブジェクトは辞書ではありません。 user_profile['beta']ではなくuser_profile.betaを使用してください。

+0

ああ、これはそれでなければならない。ありがとうございました。 –

0

また、あなたはgetattrで文字列を使用することができます。

getattr(user_profile, 'beta', False) 

Falseがデフォルト値です。あなたのケースでは、値が設定されているかどうかを確認する作業を行います。私はこれが非常に役に立ちましたので、数年前に質問されたにもかかわらず、このソリューションを投稿すると思っていました。 :)

関連する問題