2012-02-16 145 views
1

現在我在我的admin.py代碼中產生一個錯誤,當我嘗試使用django.db.models.User時,我正在猜測https://docs.djangoproject.com/en/dev/topics/auth/正在談論的地方。我的admin.py目前顯示如下:什麼是註冊用戶配置文件的正確方法?

#!/usr/bin/python 
# coding=UTF-8 

import django.contrib.admin 
from django.db.models.signals import post_save 
import django.db.models 
from pim_accounts.models import UserProfile 

django.contrib.admin.autodiscover() 

def create_user_profile(sender, instance, created, **kwargs): 
    if created: 
     UserProfile.objects.create(user = instance) 

post_save.connect(create_user_profile, sender = django.db.models.User, 
    dispatch_uid = 'create_user_profile') 

整個項目在http://JonathansCorner.com/project/pim.tgz

什麼是設置事物的正確和首選方式,以便將pim_accounts.models.UserProfile設置爲所有帳戶的用戶配置文件?

錯誤(加上環境設置)是:

AttributeError at /accounts/login/ 
    'module' object has no attribute 'User' 
Request Method: GET 
Request URL: http://localhost:8000/accounts/login/?next=/ 
Django Version: 1.3.1 
Exception Type: AttributeError 
Exception Value:  
'module' object has no attribute 'User' 
Exception Location: /Users/jonathan/pim/../pim/admin.py in <module>, line 15 
Python Executable: /usr/local/bin/python 
Python Version: 2.7.0 
Python Path:  
['/Users/jonathan/pim', 
'/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/distribute-0.6.14-py2.7.egg', 
'/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/pip-0.8.1-py2.7.egg', 
'/usr/local/Cellar/python/2.7/lib/python27.zip', 
'/usr/local/Cellar/python/2.7/lib/python2.7', 
'/usr/local/Cellar/python/2.7/lib/python2.7/plat-darwin', 
'/usr/local/Cellar/python/2.7/lib/python2.7/plat-mac', 
'/usr/local/Cellar/python/2.7/lib/python2.7/plat-mac/lib-scriptpackages', 
'/usr/local/Cellar/python/2.7/lib/python2.7/lib-tk', 
'/usr/local/Cellar/python/2.7/lib/python2.7/lib-old', 
'/usr/local/Cellar/python/2.7/lib/python2.7/lib-dynload', 
'/usr/local/Cellar/python/2.7/lib/python2.7/site-packages', 
'/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/PIL', 
'/usr/local/lib/python2.7/site-packages', 
'/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg-info'] 
Installed Applications: 
    ['django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.sites', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    'django.contrib.admin', 
    'pim', 
    'pim_accounts', 
    'pim_calendar', 
    'pim_scratchpad'] 
Installed Middleware: 
    ('django.middleware.common.CommonMiddleware', 
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.csrf.CsrfViewMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware') 
Server time: Thu, 16 Feb 2012 10:29:54 -0600 

詳細的錯誤信息

Traceback: 
File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response 
    101.        request.path_info) 
File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/core/urlresolvers.py" in resolve 
    250.    for pattern in self.url_patterns: 
File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/core/urlresolvers.py" in _get_url_patterns 
    279.   patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module) 
File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/core/urlresolvers.py" in _get_urlconf_module 
    274.    self._urlconf_module = import_module(self.urlconf_name) 
File "/usr/local/Cellar/python/2.7/lib/python2.7/site-packages/django/utils/importlib.py" in import_module 
    35.  __import__(name) 
File "/Users/jonathan/pim/../pim/urls.py" in <module> 
    2. import admin 
File "/Users/jonathan/pim/../pim/admin.py" in <module> 
    15. post_save.connect(create_user_profile, sender = django.db.models.User, 

Exception Type: AttributeError at /accounts/login/ 
Exception Value: 'module' object has no attribute 'User' 
+1

請張貼錯誤。 – jterrace 2012-02-16 16:37:53

+1

如果您有錯誤,請發佈錯誤消息。總是。如果我們不知道實際的問題是什麼,我們無法幫助你。 – 2012-02-16 16:39:11

+3

'用戶'在'django.contrib.auth.models'包中,而不是'django.db.models'。 – mipadi 2012-02-16 16:59:59

回答

1

我只是用User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])。當您嘗試訪問user.profile時,這將創建一個配置文件,如果它不存在。您還應該將AUTH_PROFILE_MODULE設置爲settings.py中用於配置文件的模型。

0

貌似你試圖從django.db.models而不是django.contrib.auth.models導入用戶,請嘗試:

from django.contrib.auth.models import User 
post_save.connect(create_user_profile, sender=User, 
    dispatch_uid='create_user_profile') 
相關問題