Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customizing an Admin form in Django while also using autodiscover

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?

like image 682
S.Lott Avatar asked Jan 23 '09 01:01

S.Lott


People also ask

Is Django admin customizable?

The Django admin is a powerful built-in tool giving you the ability to create, update, and delete objects in your database using a web interface. You can customize the Django admin to do almost anything you want.

What is admin interface in Django?

Django provides a default admin interface which can be used to perform create, read, update and delete operations on the model directly. It reads set of data that explain and gives information about data from the model, to provide an instant interface where the user can adjust contents of the application .


2 Answers

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) 
like image 74
Carl Meyer Avatar answered Sep 30 '22 07:09

Carl Meyer


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/

like image 34
Andy Baker Avatar answered Sep 30 '22 07:09

Andy Baker