1

独自の管理者 (addおよびchangeフォーム) を持つカスタム ユーザー モデルを作成しようとしています。add元のユーザー管理者とまったく同じにして、changeフォームにフィールドを追加するだけにしたいと思います。

誰でも助けることができますか?

models.py

from django.contrib.auth.models import User

class MyUser1(User):
    #add more fields specific to MyUser1

class MyUser2(User):
    #add more fields specific to MyUser2

admin.py

from django.contrib.auth.admin import UserAdmin

class MyUser1Admin(UserAdmin):
    # how can I add the "fields specific to MyUser1" both in the add
    # and the change forms?

admin.site.register(MyUser1, MyUser1Admin)


# same for MyUser2
4

2 に答える 2

2

UserCreationForm と UserChangeForm を拡張する必要がありました。

admin.py

from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django.contrib.auth.admin import UserAdmin
from django.contrib import admin
from django import forms

class MyUser1CreationForm(UserCreationForm):
    myuser1_field = forms.CharField()

    class Meta:
        model = User
        fields = ("username", "myuser1_field", "password1", "password2")

class MyUser1ChangeForm(UserChangeForm):
    myuser1_field = forms.CharField()

    class Meta:
        model = User

class MyUser1Admin(UserAdmin):

    form = MyUser1ChangeForm
    add_form = MyUser1CreationForm

    fieldsets = (
        (None, {'fields': ('username', 'password')}),
        (_('Personal info'), {'fields': ('myuser1_field', 'first_name', 'last_name', 'email')}),
        (_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
                                       'groups', 'user_permissions')}),
        (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
    )


    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('username', 'myuser1_field', 'password1', 'password2')}
        ),
    )


admin.site.register(MyUser1, MyUser1Admin)
于 2012-12-12T14:07:16.547 に答える
0
from django.contrib.auth.forms import UserChangeForm

class MyUser1AdminForm(UserChangeForm):

    class Meta:
        model = MyUser1
于 2012-12-12T11:37:33.517 に答える