30

我正在尝试将 django.contrib.auth 用户与我自己的 UserProfile 连接,当我访问该站点时出现“已注册”错误。

这是回溯:

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
4

1 回答 1

66

当您在应用程序中定义自定义用户管理员时admin.py,您必须先取消注册默认User模型管理员,然后再注册您自己的管理员。

admin.site.unregister(User)
admin.site.register(User, MyUserAdmin)
于 2012-12-04T18:12:37.947 回答