2012-02-16 114 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') 
相关问题