2012-02-16 30 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

+1

エラーが発生した場合は、エラーメッセージを投稿してください。常に。実際の問題がわからないと、私たちはあなたを助けることができません。 –

+3

'User'は' django.db.models'ではなく 'django.contrib.auth.models'パッケージにあります。 – mipadi

答えて

1

私はちょうどUser.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])を使用しています。存在しない場合は、user.profileにアクセスしようとするとプロファイルが作成されます。また、settings.pyのプロファイルに使用しているモデルにAUTH_PROFILE_MODULEを設定する必要があります。あなたがしようと、django.db.modelsではなくdjango.contrib.auth.modelsからユーザーをインポートしようとしているよう

0

はルックス:

from django.contrib.auth.models import User 
post_save.connect(create_user_profile, sender=User, 
    dispatch_uid='create_user_profile') 
関連する問題