Django.contrib.authユーザーを自分のUserProfileに接続しようとしていますが、サイトにアクセスすると「AlreadyRegistered」エラーが発生します。
トレースバックは次のとおりです。
Environment:
Request Method: GET
Request URL: myurl.com/Django/appname/
Django Version: 1.4.2
Python Version: 2.6.8
Installed Applications:
('Django.contrib.auth',
'Django.contrib.contenttypes',
'Django.contrib.sessions',
'Django.contrib.sites',
'Django.contrib.messages',
'Django.contrib.staticfiles',
'Django.contrib.admin',
'polls',
'appname',
'registration',
'Django.contrib.humanize')
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')
Traceback:
File "/usr/lib/python2.6/site-packages/Django/core/handlers/base.py" in get_response
101. request.path_info)
File "/usr/lib/python2.6/site-packages/Django/core/urlresolvers.py" in resolve
298. for pattern in self.url_patterns:
File "/usr/lib/python2.6/site-packages/Django/core/urlresolvers.py" in url_patterns
328. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/lib/python2.6/site-packages/Django/core/urlresolvers.py" in urlconf_module
323. self._urlconf_module = import_module(self.urlconf_name)
File "/usr/lib/python2.6/site-packages/Django/utils/importlib.py" in import_module
35. __import__(name)
File "/var/www/mysite/mysite/urls.py" in <module>
3. admin.autodiscover()
File "/usr/lib/python2.6/site-packages/Django/contrib/admin/__init__.py" in autodiscover
29. import_module('%s.admin' % app)
File "/usr/lib/python2.6/site-packages/Django/utils/importlib.py" in import_module
35. __import__(name)
File "/var/www/mysite/appname/admin.py" in <module>
4. admin.site.register(User)
File "/usr/lib/python2.6/site-packages/Django/contrib/admin/sites.py" in register
80. raise AlreadyRegistered('The model %s is already registered' % model.__name__)
Exception Type: AlreadyRegistered at /appname/
Exception Value: The model User is already registered
これは私のアプリのmodels.pyに由来していると確信しています。これが、私が大幅に変更したすべてであると考えています。
from Django.db import models
from Django.contrib.auth.models import User
from Django.db.models.signals import post_save
class Tag(models.Model):
pics = models.ManyToManyField('Picture', blank=True)
name = models.CharField(max_length=30)
def __unicode__(self):
return self.name
class UserProfile(models.Model):
name = models.CharField(max_length=20)
date_joined = models.DateTimeField('date joined')
auth_user = models.OneToOneField(User)
def __unicode__(self):
return self.name
def create_user_profile(sender, instance, created, **kwargs):
if created:
UserProfile.objects.create(user=instance)
post_save.connect(create_user_profile, sender=User)
class Picture(models.Model):
name = models.CharField(max_length=100)
pub_date = models.DateTimeField('date published')
tags = models.ManyToManyField('Tag', blank=True)
owner = models.ForeignKey('UserProfile')
content = models.ImageField(upload_to='instaton')
def __unicode__(self):
return self.name
class Comment(models.Model):
content = models.CharField(max_length=500)
date = models.DateTimeField('date commented')
commenter = models.ForeignKey('UserProfile')
pic = models.ForeignKey('Picture')
def __unicode__(self):
return self.content
アプリのadmin.py
でカスタムユーザー管理者を定義する場合は、独自の管理者を登録する前に、まずデフォルトのUser
モデル管理者の登録を解除する必要があります。
admin.site.unregister(User)
admin.site.register(User, MyUserAdmin)