开发者

Changing Django prebuild form to add new user

开发者 https://www.devze.com 2023-03-23 12:54 出处:网络
I was given a task to add a field \"location\" to the django prebuilt user module I found the location of the module itself and added the location field as well as the admin modules

I was given a task to add a field "location" to the django prebuilt user module I found the location of the module itself and added the location field as well as the admin modules

eg:

class UserAdmin(admin.ModelAdmin):
    add_for开发者_运维技巧m_template = 'admin/auth/user/add_form.html'
    change_user_password_template = None
    fieldsets = (
        (None, {'fields': ('username', 'password')}),
        (_('Personal info'), {'fields': ('first_name', 'last_name', 'email', 'location')}),
        (_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser', 'user_permissions')}),
        (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
        (_('Groups'), {'fields': ('groups',)}),
    )

I rebuilt the database hoping it would show up in the adding of new user form. But It didn't seem to work. So I assume I forgot to change something else. I was looking around in the html code for ages trying to find the location of where the actual "change user" form is called, but couldn't find it.

I would appreciate help from someone who dealt with the pre built django admin backend before. Thank you!


It's not recommended to modify django's user model because it complicates things. Instead, you can add a user profile as documented here.


If you want the profile to show up in the admin side, you can attach the profile as an inline and force it by unregistering the user and then re-registering it. Here's how I do it:

from django.contrib.auth.models import User
from reversion.admin import VersionAdmin
from profiles.models import Profile
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin

class ProfileInline(admin.StackedInline):
    model = Profile
    fk_name = 'user'
    extra = 0
    max_num = 1
    fieldsets = [
        ('Company', {'fields': ['company', 'office']}),
        ('User Information', {'fields': ['role', 'gender', 'phone', 'mobile', 'fax', 'street', 'street2', 'city', 'state', 'zip']}),
        ('Site Information', {'fields': ['sites']}),
    ]

class NewUserAdmin(VersionAdmin):
    inlines = [ProfileInline, ]

admin.site.unregister(User)
admin.site.register(User, NewUserAdmin)
0

精彩评论

暂无评论...
验证码 换一张
取 消