2012-01-26 1 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' 

, 난이 형식 오류를 얻을 :

(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 

테스트 된 기능 :

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 객체 생성하는 핸들러 첨부 할 것입니다

: 회원을 통해 루프에서 너무

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

을, 당신은 창조한다 User 개체가 아닌 개체인데, 이는 신호를 통해 자동으로 만들어지기 때문입니다.

당신이 (가입 절차) 암호를 얻는 방법을 정말 분명하지 않다,하지만 당신은 그이 후에는 사용하여 해당 사용자의 프로필을 업데이트 할 수 있습니다

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