Already Registered at /admin/ Django 1.2 error
django, django-admin
Solution
`admin.py` files are executed by Django during runtime so if you import an `admin.py` script into another script, you are actually executing the exposed `admin.site.register` functions again, which thus gives you the `Already Registered` error.
This is in fact standard Python behavior. Consider the following script (save as `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 do `import sample` it will write the `out.txt` output. The same case when you selectively import the `multiplier` function like `from sample import multiplier`, the output file is still written. The only way to avoid the output file being written is to comment out the executed function in the script or wrap it into another function.
Problem
I'm experiencing this error in Django 1.2 admin. Scenario: I have two applications, say `app1` and `app2` inside my project. In both of these apps, I defined their respective `admin.py` files to hook each apps respective models to django's admin site. Inside `app1` admin.py, I defined three ModelAdmin classes corresponding to three models class in app1 and registered two of them to 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 an `app2` model. inside admin.py of `app2`: ``` from app1.admin import App11stModelAdmin class App21stModelAdmin(App11stModelAdmin): #define some things here #register App21stModelAdmin to admin site admin.site.register(App21stModel, App21stModelAdmin) ``` With this code, I am getting this 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 strange because I'm sure I'm only registering that model's admin once. When I commented out the register statement for that model, I got the same error, but now for `App13rdModel` model. In the meantime, to fix this issue, I removed the registration statements and instead put them inside a '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 included the register function in the 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 this works. I am not getting the Already Registered error anymore. Question: What did I do which resulted to that error? I am new to Django and Python. Thank you very much!