google-code-export / django-mptt

Automatically exported from code.google.com/p/django-mptt
Other
0 stars 0 forks source link

The model Category has already been registered. #14

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
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.

Original issue reported on code.google.com by verbo...@gmail.com on 13 Feb 2008 at 10:22

GoogleCodeExporter commented 9 years ago
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)

Original comment by smileychris on 25 Feb 2008 at 3:26

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
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?

Original comment by walle...@gmail.com on 29 Jun 2008 at 10:19

GoogleCodeExporter commented 9 years ago
Here is my fix:

try:
    mptt.register(Page, order_insertion_by='name')
except mptt.AlreadyRegistered:
    pass

Original comment by mwdi...@gmail.com on 10 Jul 2008 at 12:50

GoogleCodeExporter commented 9 years ago
I'm getting the same issue,  except my exception isn't from mptt.register, it's:

django.contrib.admin.sites.AlreadyRegistered:

Any ideas?

Original comment by bendavi...@gmail.com on 14 Aug 2008 at 11:43

GoogleCodeExporter commented 9 years ago
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.

Original comment by marc.rem...@gmail.com on 22 Aug 2008 at 11:29

GoogleCodeExporter commented 9 years ago
@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).

Original comment by houzi1...@gmail.com on 16 Dec 2008 at 4:33

GoogleCodeExporter commented 9 years ago
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...

Original comment by johanneswilm on 30 Dec 2008 at 9:59

GoogleCodeExporter commented 9 years ago
Put the registration at the end of the file, after every Model definition.

Original comment by hubscher.remy on 19 Nov 2009 at 7:48

GoogleCodeExporter commented 9 years ago
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'])

Original comment by ishalya...@gmail.com on 23 Apr 2010 at 8:36

GoogleCodeExporter commented 9 years ago
Is this still a problem? Why are people calling register() outside of models.py?

Original comment by craig.ds@gmail.com on 5 Sep 2010 at 12:42

GoogleCodeExporter commented 9 years ago
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.

Original comment by hubscher.remy on 5 Sep 2010 at 8:13

GoogleCodeExporter commented 9 years ago
Fixed in 
http://github.com/django-mptt/django-mptt/commit/37684dc4c10ff847b4ccbcb7266fba9
752d7ad22

Original comment by craig.ds@gmail.com on 5 Sep 2010 at 10:39