| Issue 14: | The model Category has already been registered. | |
| 6 people starred this issue and may be notified of changes. | Back to list |
1. start e new app on a fresh database, create a Category model and register it with mptt.Register 2. start the admin site 3. the home page of the admin, after login, will show a model already registered error I’m using django-mptt trunk on django trunk, Mac OS X 10.4.11 with Sqlite 3.3.6.
Feb 24, 2008
#1
smileychris
Jun 29, 2008
I ran into this too, but was not able to work around it by creating a register.py file and calling it there. How exactly did you do this? I have commented out the line that throws an AlreadyRegistered exception and replaced it with a return in the register function in mptt/__init__.py. This seems to work. Am I misunderstanding how mptt.register should be used? Should I have just called this once from the django python shell before running syncdb?
Jul 9, 2008
Here is my fix:
try:
mptt.register(Page, order_insertion_by='name')
except mptt.AlreadyRegistered:
pass
Aug 14, 2008
I'm getting the same issue, except my exception isn't from mptt.register, it's: django.contrib.admin.sites.AlreadyRegistered: Any ideas?
Aug 22, 2008
Calling register should be done from admin.py. It is not enforced by Django but is considered good practice. Registering in models.py breaks things not only for mptt, but in various other places. When using admin.autoload(), you even don't have to manually include admin.py somewhere to get it running.
Dec 16, 2008
@Original issue:
This seems to happen when you reference the same file in different ways in multiple
places. To illustrate, I had a project called "mptt_test" with an app called "woo".
woo/admin.py
------------
from mptt_test.woo.models import Genre
admin.site.register(Genre)
settings.py
-----------
INSTALLED_APPS = (
...
'woo',
)
Gets the mptt.register call in my woo/models.py called twice. Changing the 'woo'
above in settings.py to 'mptt_test.woo' so that it is consistent over the entire app
fixes the problem.
(I've had a similar problem in django where I hooked signals in an app's __init__.py,
and the signal handler was being registered twice. Took a while to track down).
Dec 30, 2008
I have tried for about a day now to get this to work. It seems that if the registration isn't in models.py, the required database tables aren't created. However if the registration is in models.py during runtime so you have to move it to admin.py then, this error is thrown up. If one uses the work-around described above with the "except mptt.AlreadyRegistered:" it at first looks like it's ok, but then once one tries to add another node to one of the models that are registered with mptt, it crashes with another error message. Has noone here since figured out how to fix this? Moving the registration around whenever registering it doesn't seem to be the most logical thing...
Nov 18, 2009
Put the registration at the end of the file, after every Model definition.
Apr 23, 2010
I figured out mptt.registry contains registred modules, so I use next code:
import mptt
if not mptt.registry.__contains__(Category):
mptt.register(Category, order_insertion_by=['name'])
Sep 4, 2010
Is this still a problem? Why are people calling register() outside of models.py?
Status:
Chatting
Sep 5, 2010
Yes it's append often. When you are loading a Models from another apps and if there is a mptt register in the loaded models.
But we use either :
import mptt
if not mptt.registry.__contains__(Category):
mptt.register(Category, order_insertion_by=['name'])
or
try:
mptt.register(Page, order_insertion_by='name')
except mptt.AlreadyRegistered:
pass
And it works fine.
Sep 5, 2010
Fixed in http://github.com/django-mptt/django-mptt/commit/37684dc4c10ff847b4ccbcb7266fba9752d7ad22
Status:
Fixed
|