Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with Django admin registering an inline user profile admin

I'm currently working on a django project. I'm attempting to add a UserProfile model inline to my User model. In my models.py I have:

class UserProfile(models.Model):
    '''
    Extension to the User model in django admin.
    '''
    user = models.ForeignKey(User)
    site_role = models.CharField(max_length=128, choices=SITE_ROLE)
    signature = models.CharField(max_length=128)
    position_title = models.CharField(max_length=128)
    on_duty = models.BooleanField(default=False)
    on_duty_order = models.IntegerField()

In my admin.py I have:

class UserProfileInline(admin.StackedInline):
    model = UserProfile

class UserAdmin(admin.ModelAdmin):
    inlines = [UserProfileInline]


admin.site.unregister(User)
admin.site.register(User, UserAdmin)

When I run the development server (yes, I have restarted it) I get the following exception:

NotRegistered at /admin
The model User is not registered

This exception is coming from the admin.site.unregister(User) line.

However, when I comment out that line, I get the following exception:

AlreadyRegistered at /admin
The model User is already registered

Something about my django setup seems to be a little bi-polar. I've spent an hour or so researching this problem and the code I have seems to work great for others. Does anyone have any insight into why this might be happening?

Thanks, Travis

like image 850
TravFisch Avatar asked Sep 16 '10 19:09

TravFisch


People also ask

Why Django admin is not working?

'django-admin' is not recognized as an internal or external command, operable program or batch file. To fix this, first close the terminal window and relaunch it with administrator privileges. Once you launch the elevated terminal window change directory to where you wish to start your Django project.

What does admin site register do in Django?

The Django admin is an automatically-generated user interface for Django models. The register function is used to add models to the Django admin so that data for those models can be created, deleted, updated and queried through the user interface.


1 Answers

my guess is that you either are doing some crazy module importing... or... you have an ordering problem in your settings.INSTALLED_APPS list. Make sure that 'django.contrib.auth' appears on your list before your app that is replacing the default admin. The list should look something like this:

INSTALLED_APPS = (
    # django apps first
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.admin',

    # your stuff from here on
    'yourproject.userstuff',
)

That way django's app registers the User model, and then you unregister and re-register it with your own ModelAdmin.

like image 198
Federico Cáceres Avatar answered Oct 11 '22 08:10

Federico Cáceres