Próbuję połączyć użytkownika django.contrib.auth z moim własnym UserProfile i otrzymuję błąd "AlreadyRegistered", gdy wejdę na stronę .Już zarejestrowany w/appname /: Model użytkownika jest już zarejestrowany
Oto traceback:
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
Jestem całkiem pewien, że to wynika z mojej aplikacji models.py, skoro to wszystko, co zmieniło się w sposób istotny, wierzę.
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
Stało się tak również, gdy zmodyfikowałem aplikację, aby użyć oryginalnego obiektu auth.User zamiast niestandardowego, źle nazwanego obiektu użytkownika. Dlatego mój problem polegał na próbie zarejestrowania obiektu auth.User, gdy jest już zarejestrowany. Usunięcie wywołania register (User) naprawiło to. – brodney