django错误:“抓住已经注册”?仅注册模型一次

时间:2012-03-01 20:10:10

标签: django django-models django-admin

我在开发环境中遇到此错误。我花了很长时间试图解决这个问题,特别是因为我想它正盯着我看。我针对我的一个模型创建了一个新的ModelForm,然后开始出现这个错误。如果我在models.py文件中注释掉“admin.site.register(ModelName)”行,它就会消失,但我不能再检查管理站点中的数据...

以下是错误:

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/admin/

Django Version: 1.3.1
Python Version: 2.7.2
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'django.contrib.admindocs',
 'azcerts.certmanager']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.middleware.csrf.CsrfResponseMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  101.                             request.path_info)
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in resolve
  250.             for pattern in self.url_patterns:
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in _get_url_patterns
  279.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in _get_urlconf_module
  274.             self._urlconf_module = import_module(self.urlconf_name)
File "C:\Python27\lib\site-packages\django\utils\importlib.py" in import_module
  35.     __import__(name)
File "C:\Users\ntagg\Code\azcerts\urls.py" in <module>
  3. from certmanager.views import addone
File "C:\Users\ntagg\Code\azcerts\certmanager\views.py" in <module>
  4. from forms import CertForm
File "C:\Users\ntagg\Code\azcerts\forms.py" in <module>
  4. from certmanager.models import UserProfile, Cert
File "C:\Users\ntagg\Code\azcerts\certmanager\models.py" in <module>
  52. admin.site.register(UserProfile)
File "C:\Python27\lib\site-packages\django\contrib\admin\sites.py" in register
  85.                 raise AlreadyRegistered('The model %s is already registered' % model.__name__)

Exception Type: AlreadyRegistered at /admin/
Exception Value: The model UserProfile is already registered

这是models.py:

#models.py
from django.db import models
from django.contrib import admin
from django.contrib.auth.models import User
from django.contrib.localflavor.us.models import USStateField

class Cert(models.Model):
    # My fields are here...

    def __unicode__(self):
        return self.fieldname

class UserProfile(models.Model):
    # My fields are here...

    def __unicode__(self):
        return self.fieldname

admin.site.register(UserProfile)
admin.site.register(Cert)

1 个答案:

答案 0 :(得分:5)

请勿在{{1​​}}注册您的模型,为您的应用创建models.py,并在那里进行。

admin.py

说明:

当您创建模型表单时,Django会导入您的# admin.py from django.contrib import admin from myapp.models import Cert, UserProfile admin.site.register(Cert) admin.site.register(UserProfile) 两次:一次使用路径models.py(就像在azcerts.certmanager.models中一样)并再次使用路径{{1 (在您定义模型表单的模块中)。 Django第二次导入models.py,它试图再次注册模型,并给出了错误。

通过将register命令移动到admin.py,可以确保模型只能注册一次。

INSTALLED_APPS可以在这样的两条不同路径上导入,这有点乱。 Django 1.4中的默认项目布局已经改变,这将阻止这样的陷阱。