Importing a model from one app into another app causes "AlreadyRegistered" exception.
|Reported by:||Owned by:||Brian Rosner|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
in a project with two apps, app1 and app2, with the following models.py files:
from django.db import models from django.contrib import admin class MyFirstModel(models.Model): pass admin.site.register(MyFirstModel) ...
from django.db import models from django.contrib import admin from app1.models import MyFirstModel ...
running the command:
raises the following exception:
Unhandled exception in thread started by <function inner_run at 0x2ab13a637c80> Traceback (most recent call last): ... File "/usr/local/lib/python2.4/site-packages/django/contrib/admin/sites.py", line 82, in register raise AlreadyRegistered('The model %s is already registered' % model.__name__) django.contrib.admin.sites.AlreadyRegistered: The model MyFirstModel is already registered
My quick and dirty solution is to get rid of the exception in the register method of the AdminSite class in the sites.py file. i.e. changing it from this:
for model in model_or_iterable: if model in self._registry: raise AlreadyRegistered('The model %s is already registered' % model.__name__) self._registry[model] = admin_class(model, self)
for model in model_or_iterable: if not model in self._registry: self._registry[model] = admin_class(model, self)
In this way models are only registered if they haven't already been registered, and are ignored otherwise (instead of raising an exception) - as far as I can tell this shouldn't be a problem - but I'm not familiar enough with the code and your objectives to know for sure.
Change History (14)
comment:1 Changed 9 years ago by
|Patch needs improvement:||unset|
|Triage Stage:||Unreviewed → Design decision needed|
comment:6 Changed 8 years ago by
|Owner:||changed from nobody to Brian Rosner|
|Status:||new → assigned|
|Triage Stage:||Design decision needed → Accepted|