简体   繁体   English

使用OneToOne字段保存模型

[英]Save model with OneToOne field

I have created a profile-model to extend the default django user (using django 1.6) But I can't save the profile-model correctly. 我创建了一个配置文件模型来扩展默认的django用户(使用django 1.6),但是我无法正确保存该配置文件模型。

Here is my model: 这是我的模型:

from django.contrib.auth.models import User

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

Here is my celery-task which updates personrecords from a wdsl file: 这是我的celery任务,它从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()

The django_user.save() is working fine, but the profile_user.save() is not working. django_user.save()工作正常,但profile_user.save()无法工作。 Im getting this error: AttributeError: 'tuple' object has no attribute 'mobilephone' 我收到此错误: AttributeError: 'tuple' object has no attribute 'mobilephone'

Anyone see what Im doing wrong? 有人看到我在做什么错吗?

I have found 2 bugs in your code: 我在您的代码中发现了2个错误:

  • get_or_create method returns tuple (objects, created), so you have to change your code to: get_or_create方法返回元组(已创建对象),因此您必须将代码更改为:

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

    or, if you need information about state of returned object (just created or not) you should use 或者,如果您需要有关返回对象的状态(无论是否创建)的信息,则应使用

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

    and then rest of code will work correctly. 然后其余的代码将正常工作。

  • In your profile model the field models.CharField must have max_length argument declared. 在您的概要模型中,字段models.CharField必须声明了max_length参数。

  • You don't need to use () in @task decorator. 您无需在@task装饰器中使用() You only have to do this if you pass arguments to the decorator. 仅在将参数传递给装饰器时才需要这样做。

  • Also you can avoid build user profile with one to one database connection using django custom user model . 您也可以避免使用django自定义用户模型通过一对一的数据库连接来构建用户配置文件。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM