2013-10-11 5 views
0

기본 django 사용자 (django 1.6 사용)를 확장하기 위해 프로파일 모델을 만들었습니다 그러나 프로파일 모델을 올바르게 저장할 수 없습니다.OneToOne 필드로 모델 저장

여기 내 모델 :

from django.contrib.auth.models import User 

class Profile(models.Model): 
    user = models.OneToOneField(User) 
    mobilephone = models.CharField(max_length=20, blank=True) 

여기에 WDSL 파일에서 personrecords을 업데이트 내 셀러리-작업은 다음과 같습니다

@task() 
def update_local(user_id): 

    url = 'http://webservice.domain.com/webservice/Person.cfc?wsdl' 

    try: 
     #Make SUDS.Client from WSDL url 
     client = Client(url) 
    except socket.error, exc: 
     raise update_local.retry(exc=exc) 
    except BadStatusLine, exc: 
     raise update_local.retry(exc=exc) 


    #Make dict with parameters for WSDL query 
    d = dict(CustomerId='xxx', Password='xxx', PersonId=user_id) 

    try: 
     #Get result from WSDL query 
     result = client.service.GetPerson(**d) 
    except (socket.error, WebFault), exc: 
     raise update_local.retry(exc=exc) 
    except BadStatusLine, exc: 
     raise update_local.retry(exc=exc) 



    #Soup the result 
    soup = BeautifulSoup(result) 


    #Firstname 
    first_name = soup.personrecord.firstname.string 

    #Lastname 
    last_name = soup.personrecord.lastname.string 

    #Email 
    email = soup.personrecord.email.string 

    #Mobilephone 
    mobilephone = soup.personrecord.mobilephone.string 



    #Get the user  
    django_user = User.objects.get(username__exact=user_id) 

    #Update info to fields 
    if first_name: 
     django_user.first_name = first_name.encode("UTF-8") 

    if last_name:  
     django_user.last_name = last_name.encode("UTF-8") 

    if email: 
     django_user.email = email 


    django_user.save() 



    #Get the profile  
    profile_user = Profile.objects.get_or_create(user=django_user) 

    if mobilephone: 
     profile_user.mobilephone = mobilephone 

    profile_user.save() 

django_user.save()가 잘 작동하지만, profile_user.save()가 작동하지 않습니다. 임 :이 오류가 발생했습니다. AttributeError: 'tuple' object has no attribute 'mobilephone'

누구든지 무엇이 잘못 되었습니까?

답변

6

내가 코드에서이 버그를 발견 : 당신이 당신의 코드를 변경해야

  • get_or_create 방법은 반환 튜플 (객체 생성) : 그래서,

    profile_user = Profile.objects.get_or_create(user=django_user)[0]

    또는, 반환 된 객체의 상태에 대한 정보가 필요하다면 (작성한 지 아닌지)

    profile_user, created = Profile.objects.get_or_create(user=django_user)

    그리고 나머지 코드는 올바르게 작동합니다.

  • 귀하의 프로파일 모델에서 필드 models.CharFieldmax_length 인수가 선언되어야합니다.

  • 장식 자 @task에는 ()을 사용할 필요가 없습니다. 데코레이터에 인수를 전달하는 경우에만이 작업을 수행해야합니다.

  • 또한 django custom user model을 사용하여 일대일 데이터베이스 연결로 사용자 프로필을 작성하지 않아도됩니다.

희망이 있습니다.

+0

좋아요! 내 질문에 max_length를 잊어 버렸지만 내 코드에는 없습니다. –

+0

나는 몰랐다. 그러나이 매개 변수없이 django 서버를 실행할 수 없기 때문에 이상하게 보였다 (모델 유효성 검사는 실패 할 것이다) – dydek

관련 문제