django使用表格来更新管理员用户

时间:2019-12-19 07:17:55

标签: django forms admin

我有一个用于注册的表格,我想用它来填写管理页面中的用户信息

forms.py

from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User


class SignUpForm(UserCreationForm):
    first_name = forms.CharField(max_length=30, required=False, help_text='Optional.')
    last_name = forms.CharField(max_length=30, required=False, help_text='Optional.')
    email = forms.EmailField(max_length=254, help_text='Required. Inform a valid email address.')
    position = forms.CharField(max_length=30, required=False, help_text='Optional.')
    store = forms.CharField(max_length=30, required=False, help_text='Optional.')


    #making it only allow @gmail.org emails
    def clean_email(self):
        data = self.cleaned_data['email']
        if "@gmail.com" not in data:   # any check you need
            raise forms.ValidationError("Must be a valid email.")
        return data




    class Meta:
        model = User
        fields = ('username', 'first_name', 'last_name', 'email', 'position', 'store', 'password1', 'password2',  )
        exclude = ('user',)

然后我就厌倦了使用此功能将其添加到管理员用户

admin.py

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from .models import Category, Employee, Manager
from .forms import SignUpForm


admin.site.site_header = "Employee Management System"


class ProfileInline(admin.StackedInline):
    form = SignUpForm
    can_delete = False
    verbose_name_plural = 'SignUpForm'
    fk_name = 'user'


class CustomUserAdmin(UserAdmin):
    inlines = (ProfileInline, )
    list_display = ('username', 'first_name', 'last_name', 'email', 'first_name', 'last_name', 'is_staff', 'position',
                    'store')
    list_select_related = ('SignUpForm', )

    def get_inline_instances(self, request, obj=None):
        if not obj:
            return list()
        return super(CustomUserAdmin, self).get_inline_instances(request, obj)


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

我不认为这在管理员方面是正确的,因为我正在遵循向页面添加新字段的教程,无论如何,我们将不胜感激

0 个答案:

没有答案