views:

1786

answers:

2

I want to modify a few tiny details of Django's built-in django.contrib.auth module. Specifically, I want a different form that makes username an email field (and email an alternate email address. (I'd rather not modify auth any more than necessary -- a simple form change seems to be all that's needed.)

When I use autodiscover with a customized ModelAdmin for auth I wind up conflicting with auth's own admin interface and get an "already registered" error.

It looks like I have to create my own admin site, enumerating all of my Models. It's only 18 classes, but it seems like a DRY problem -- every change requires both adding to the Model and adding to the customized admin site.

Or, should I write my own version of "autodiscover with exclusions" to essentially import all the admin modules except auth?

+14  A: 

None of the above. Just use admin.site.unregister(). Here's how I recently added filtering Users on is_active in the admin (n.b. is_active filtering is now on the User model by default in Django core; still works here as an example), all DRY as can be:

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User

class MyUserAdmin(UserAdmin):
    list_filter = UserAdmin.list_filter + ('is_active',)

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

I think this probably requires that whatever app this code lives in be listed after django.contrib.auth in INSTALLED_APPS. Not actually sure, though, I haven't tried it the other way.

Carl Meyer
Sweet -- totally missed the unregister method in `sites.AdminSite`
S.Lott
Interesting. Thanks for sharing, Carl.
ayaz
thanks! _unregister()_ very useful!
ramusus
I note that, after adding this unregister() call to my code, I had to manually kill and re-start my development server before Django "saw" the change I had made. Which made me waste 15 minutes trying to figure out why unregister() wasn't working. Which finally brought me to this Stack Overflow question, where I discovered that what I was doing *was* supposed to work. :-)
Brandon Craig Rhodes
@Brandon Yeah, in general Django's admin.py autodiscovery does not play well with the dev server auto-reloading. This'll also bite you if you have a syntax error in an admin.py; after you fix it, the error will go away but that app will be missing entirely from the admin until you manually restart the dev server. Irritating, but I haven't yet dug in to find the bug.
Carl Meyer
+1  A: 

I think it might be easier to do this with a custom auth backend and thus remove the need for a customized ModelAdmin.

I did something similar with this snippet: http://www.djangosnippets.org/snippets/74/

andybak
Actually, I need both the enhanced backend and the front-end. I need to have the usernames be email addresses so that the "@domain" assures they're unique. I'm already using your snippet. But it helps to have it as part of this answer set.
S.Lott
Actually I should point out that it wasn't my snippet.
andybak