I am getting this error in my development environment. I have spent far too long trying to figure this out, especially because I imagine it’s staring me in the face. I created a new ModelForm against one of my models and then started getting this error. It goes away if I comment out the “admin.site.register(ModelName)” lines in my models.py file, but then I can’t inspect the data in the admin site anymore…
Here’s the error:
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
Here’s the 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)
Don’t register your models in your
models.py, create anadmin.pyfor your app, and do it there.Explanation:
When you created your model form, Django was importing your
models.pytwice: once with the pathazcerts.certmanager.models(as it is in yourINSTALLED_APPS) and again with the pathcertmanager.models(in the module where you define the model form). The second time Django imported models.py, it tried to register the models again, and gave the error.By moving the register commands into admin.py, you ensure that the models will only be registered once.
It’s a bit messy that
models.pycan be imported on two different paths like this. The default project layout has changed in Django 1.4, which will prevent gotchas like this.