Issue 14: The model Category has already been registered.
Status:  Fixed
Owner: ----
Closed:  Sep 2010
Reported by verbo...@gmail.com, Feb 13, 2008
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
I've encountered the same thing before -- it happens when you import the models
module from multiple places. Newforms-admin has a similar problem with models getting
registered more than once.

The solution I'm using is to do the registrations in a separate module (call it
register.py or something) and import that from your app's __init__.py (import register)
Jun 29, 2008
#3 walle...@gmail.com
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
#4 mwdi...@gmail.com
Here is my fix:

try:
    mptt.register(Page, order_insertion_by='name')
except mptt.AlreadyRegistered:
    pass
Aug 14, 2008
#5 bendavi...@gmail.com
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
#6 marc.rem...@gmail.com
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
#7 houzi1...@gmail.com
@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
#8 johanneswilm
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
#9 hubscher.remy
Put the registration at the end of the file, after every Model definition.
Apr 23, 2010
#10 ishalya...@gmail.com
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
Project Member #11 craig.ds@gmail.com
Is this still a problem? Why are people calling register() outside of models.py?
Status: Chatting
Sep 5, 2010
#12 hubscher.remy
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.