2012-01-26 7 views
1

私のUserProfileのフィールドにデータを保存しようとすると、Djangoは型エラーを発生させます。djangoのuserProfileのフィールドを更新する方法

私はUserProfileを正しく拡張したと思っていました。

マイUSERPROFILEモデル:私の設定で

class UserProfile(models.Model): 
    user = models.OneToOneField(User, unique=True, related_name='profile') 
    .... 
    initials = models.CharField(max_length=5) 

User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0]) 

私が追加しました:

AUTH_PROFILE_MODULE = 'timepiece.UserProfile' 

私は、ユーザーから継承したフィールドに値を挿入する必要があり、テストを実行すると、私はTypeError例外を取得:

(erp)BAir:website jorrit$ ./manage.py test timepiece.PivotalTest.test_update_users 
Creating test database for alias 'default'... 
E 
====================================================================== 
ERROR: test_update_users (timepiece.tests.pivotal.PivotalTest) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "/Users/jorrit/virtualenvs/erp/erp/apps/timepiece/tests/pivotal.py", line 73, in test_update_users 
    UserProfile.sync_pivotal_users(self.tracker, 450001) 
    File "/Users/jorrit/virtualenvs/erp/erp/apps/timepiece/models.py", line 44, in sync_pivotal_users 
    initials = m.initials,) 
    File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 365, in __init__ 
    raise TypeError("'%s' is an invalid keyword argument for this function" % kwargs.keys()[0]) 
TypeError: 'username' is an invalid keyword argument for this function 

---------------------------------------------------------------------- 
Ran 1 test in 1.857s 

テストされた機能:

メンバーを通して、あなたのループでとても

The method get_profile() does not create a profile if one does not exist. You need to register a handler for the User model's django.db.models.signals.post_save signal and, in the handler, if created is True, create the associated user profile

、あなたが作成する必要があります:

def sync_pivotal_users(tracker, project_id): 
    members = tracker.get_memberships_project(project_id) 

    for m in members: 
    #check if member exist in db 
     try: 
      #to know if m.id exists in current db 
      UserProfile.objects.get(pivotal_member_id=m.id) 
      #if not then create a new user 
     except UserProfile.DoesNotExist: 
      u = UserProfile(pivotal_member_id = m.id, 
          username = m.name, #code breaks here 
          email = m.email, # and here 
          initials = m.initials,) 
      u.save() 

答えて

0

the approach outlined in the documentationの背後にある考え方は、新しいUserオブジェクトが作成されたときにということで、あなたは順番にUserProfileオブジェクトを作成することにハンドラをアタッチUserオブジェクトであり、オブジェクトではありません。シグナルを介して自動的に作成されるためです。

あなたが(サインアッププロセス)パスワードを取得しているかそれは本当に明確ではないですが、あなたがそれらを持っていたら、あなたが使用してそのユーザーのプロファイルを更新することができます

profile = current_user_obj.get_profile() 
profile.initial = "some value" 
profile.save()