2012-03-07 97 views
0

我遵循以下指南:http://www.turnkeylinux.org/blog/django-profile它工作得很好,除了我似乎無法將ForeignKey保存到用戶配置文件。UserProfile模型不保存ForeignKeys


模型

PCBuild模型

from django.contrib.auth.models import User 
from django.db import models 

class PCBuild(models.Model): 
    name = models.CharField(max_length=50) 
    owner = models.ForeignKey(User) 

用戶配置模型

import datetime 
import md5 

from apps.pcbuilder.models import PCBuild 
from django.contrib.auth.models import User 
from django.db import models 

class UserProfile(models.Model): 
    user = models.OneToOneField(User) 
    email_hash = models.CharField(max_length=200) #MD5 hash of e-mail 

    current_build = models.ForeignKey(PCBuild, 
     related_name='current_build', null=True, blank=True) 

    def __unicode__(self): 
     return self.user.email 


User.profile = property(lambda u: UserProfile.objects.get_or_create(
          user=u, 
          email_hash=md5.new(u.email).hexdigest())[0]) 


的問題

>>> from django.contrib.auth.models import User 
>>> from apps.pcbuilder.models import PCBuild 
>>> from django.shortcuts import get_object_or_404 

>>> user = get_object_or_404(User, pk=2) 
>>> user 
    <User: Trevor> 

>>> pcbuild = get_object_or_404(PCBuild, pk=11) 
>>> pcbuild 
    <PCBuild: StackOverflowBuild> 

>>> pcbuild.owner 
    <User: Trevor> 

>>> user.profile.email_hash 
    u'd41d8cd98f00b204e9800998ecf8427e' 

>>> user.profile.current_build = pcbuild 
>>> user.profile.save() 
>>> user.profile.current_build 
    # nothing is stored/outputted - this is the problem! 


我是新來的Django,儘管谷歌一直樂於助人,到目前爲止,這個我還沒幾個小時後征服例。如果需要更多關於這個問題的信息,我會很樂意提供!

謝謝。


編輯:

事情我已經發現,可能是有用的(但並沒有解決我的具體問題):

回答

1

你會遇到神祕的錯誤使用該屬性。由於UserProfileUser的關係爲OneToOneField,因此可以通過執行user.userprofile從實例訪問配置文件實例。如果您想更改名稱,您可以更新OneToOneFieldrelated_name屬性。當像這樣在創建用戶

使用信號來自動創建配置文件:

from django.contrib.auth.models import User 
from django.db.models.signals import post_save 

def create_profile(sender, **kwargs): 
    """Create a UserProfile instance when a User is created.""" 
    u = kwargs['instance'] 
    if kwargs['created']: 
     UserProfile.objects.get_or_create(
         user=u, 
         email_hash=md5.new(u.email).hexdigest()) 

post_save.connect(create_profile, sender=User) 
+0

謝謝!工作很好。現在我必須閱讀信號。 – 2012-03-08 01:05:21

+0

@TrevorSenior:查看我的更新。 – sdolan 2012-03-08 01:32:05

+0

啊謝謝。我已經繼續並且改變了從'user.profile'到'user.get_profile()'的所有內容,但是如果我想稍後更改名稱,我會注意。 – 2012-03-08 01:44:32

0

我想通了 - 但我打開其他答案仍然&將稍後檢查。我很確定它與我用來獲取配置文件的lambda函數有關。我在Python shell如何去一下吧:

>>> from django.contrib.auth.models import User 
>>> from apps.pcbuilder.models import PCBuild 
>>> from django.shortcuts import get_object_or_404 
>>> user = get_object_or_404(User, pk=2) 
>>> user 
    <User: Trevor> 

>>> pcbuild = get_object_or_404(PCBuild, pk=11) 
>>> pcbuild 
    <PCBuild: StackOverflowBuild> 

>>> user.profile.current_build 
    # nothing (which is expected as there is nothing 
    # yet in the current_build of this user profile) 


# NOTE: I'm creating a new "profile" variable that holds the user profile. 

>>> profile = user.profile 
>>> profile 
    <UserProfile: Trevor> 

>>> profile.current_build 
    # nothing (which is expected as there is nothing 
    # yet in the current_build of this user profile) 

>>> profile.current_build = pcbuild 
>>> profile.save() 

>>> user.profile.current_build 
    <PCBuild: StackOverflowBuild> 

    # As you can see, it worked! 

我基本上分配user.profile到它自己的變量(輪廓中的例子),然後修改變量,保存,與其後的用戶對象。

它似乎有點尷尬,但它的工作原理。

+1

那將是非常混亂/容易發生錯誤的路線。只需刪除該屬性並使用'user.get_profile()'。 – sdolan 2012-03-08 00:29:31

+0

@sdolan我想我的問題是,如果用戶還沒有UserProfile(lambda函數的目的)呢?我會看看如果使用'user.get_profile()'爲配置文件的用戶提供了技巧。謝謝。 – 2012-03-08 00:37:31

+0

是的,那效果更好。隨意發佈它作爲答案,我會接受它。我知道lambda函數有點腥。 – 2012-03-08 00:48:21