0

デフォルトの django ユーザー (django 1.6 を使用) を拡張するプロファイル モデルを作成しましたが、プロファイル モデルを正しく保存できません。

これが私のモデルです:

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'

誰かが私が間違っていることを見ていますか?

4

1 に答える 1

7

あなたのコードに 2 つのバグが見つかりました:

  • 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.CharFieldている必要があります。max_length

  • デコレータで使用()する必要はありません。@taskデコレータに引数を渡す場合にのみ、これを行う必要があります。

  • また、 django custom user modelを使用して、1 対 1 のデータベース接続でユーザー プロファイルを作成することを回避できます。

お役に立てれば。

于 2013-10-11T13:16:56.623 に答える