Already Registered at /appname/: The model User is already registered

River Tam picture River Tam · Dec 4, 2012 · Viewed 18.9k times · Source

I'm trying to connect the django.contrib.auth User with my own UserProfile, and I'm getting an 'AlreadyRegistered' error when I go on the site.

Here's the 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

I'm pretty sure this stems from my app's models.py, seeing as that's all I changed in a major way, I believe.

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

Answer

Alasdair picture Alasdair · Dec 4, 2012

When you define a custom user admin in your app's admin.py, you must first unregister the default User model admin before registering your own.

admin.site.unregister(User)
admin.site.register(User, MyUserAdmin)