django: Resolving “The model ‘MyModel’ is already registered” error

If you are migrating to django 1.0, youll have to update your models.py for django newforms-admin

Before

from django.db import models
class MyModel(models.Model):
      """
      field definitions
      """
 
      class Admin
            pass

Now

from django.db import models
from django.contrib import admin
 
class MyModel(models.Model):
      """
      field definitions
      """
class MyModelAdmin(admin.ModelAdmin):
      """
      admin options
      """
admin.site.register(MyModel, MyModelAdmin)

When you run the server (manage.py runserver) you may get an error like,

ImproperlyConfigured at / 
Error while importing URLconf 'myproject.urls': The model MyModel is already registered

This is because the models.py is (usually) imported multiple times in your project. A model can only be associated with one AdminModel class so you get the error. To fix this, you need to place the admin.site.register line in a separate file called amdin.py. This file should exists at the same level as models.py. This will ensure that admin class for a model is registered only once.

Related posts:

  1. django: Hacking django object_list to take list of templates

4 Comments

  1. William McVey says:

    Just to be clear, it’s now recommended to put both the registration as well as the ModelAdmin sub-class definitions into ‘admin.py’. (note that your original blog has what I assume is a typo: amdin.py)

  2. Adil Saleem says:

    William,
    You are correct. Thanks for identifying the typo

  3. martin says:

    thanks this was very useful and clear!

Leave a Reply