Already registered in / admin / Django 1.2

I am experiencing this error in the Django 1.2 admin.

Scenario:

I have two applications, for example app1 and app2 inside my project. In both of these applications, I defined their respective admin.py files to connect each of them to the corresponding django admin sites.

Inside app1 admin.py, I defined three ModelAdmin classes corresponding to the three model classes in app 1, and registered two of them on the admin site.

 class App11stModelAdmin (admin.ModelAdmin): #class definitions here #This class is an abstract class #class Meta: # abstract = True class App12ndModelAdmin (admin.ModelAdmin): #class definitions here class App13rdModelAdmin (admin.ModelAdmin): #class definitions here #register to admin site two of them admin.site.register(App12ndModel, App12ndModelAdmin) admin.site.register(App13rdModel, App13rdModelAdmin) 

Inside app2 I imported app1.App11stModelAdmin to define the admin model of the app2 model.

inside admin.py app2 :

 from app1.admin import App11stModelAdmin class App21stModelAdmin(App11stModelAdmin): #define some things here #register App21stModelAdmin to admin site admin.site.register(App21stModel, App21stModelAdmin) 

Using this code, I get the following error message:

 AlreadyRegistered at /admin/ The model App12ndModel is already registered Request Method: GET Request URL: http://127.0.0.1:8000/admin/ Django Version: 1.2 Exception Type: AlreadyRegistered Exception Value: The model App12ndModel is already registered 

This is strange because I'm sure I only register the model administrator once. When I commented out the register instruction for this model, I got the same error, but now for the App13rdModel model.

In the meantime, to fix this problem, I deleted the registration statements and instead placed them inside the β€œstatic” function inside app1 admin.py.

like: inside app1 admin.py

 def register(): admin.site.register(App12ndModel, App12ndModelAdmin) admin.site.register(App13rdModel, App13rdModelAdmin) 

and then in app2 admin.py

I have included the register function in import:

 from app1.model import App11stModelAdmin, register ...... ...... #register the two admin model in app1 inside app2 admin.py by calling the register function register() 

And it works. I no longer get an already registered error.

Question:

What did I do that led to this error? I am new to Django and Python.

Many thanks!

+4
source share
2 answers

admin.py files are executed by Django at run time, so if you import the admin.py script into another script, you again run the public functions admin.site.register , which gives you an Already Registered error.

This is actually standard Python behavior. Consider the following script (excluding sample.py ):

 def multiplier(x,y): return x*y def sample_write(text): out = open("out.txt","w") out.write(text) out.close() sample_write("hey") # Notice this executed function 

When you open your Python interpreter in the same directory and execute import sample , it will write out.txt output. In the same case, when you selectively import a multiplier function, such as from sample import multiplier , the output file is still written. The only way to avoid writing the output file is to comment on the executed function in the script or transfer it to another function.

+5
source

The only thing that can cause this is to import admin.py in two ways. For example, the following is considered as two different modules and will actually cause the admin.py code to run twice:

 from foo.admin import FooAdmin from myproject.foo.admin import FooAdmin 
+3
source

All Articles