简体   繁体   中英

Create Superuser in Django Custom Auth Model

I want to create a superuser in my custom Authorization User model. Here is my models.py

class User(AbstractUser):
    is_Admin = models.BooleanField(default=False)
    is_HR = models.BooleanField(default=False)
    is_MGR = models.BooleanField(default=False)
    is_EMP = models.BooleanField(default=True)


class Admins(models.Model):

    def __str__(self):
        return self.user.username

    user = models.OneToOneField(User, on_delete=models.CASCADE)
    user.is_admin = True
    first_name = models.CharField(max_length=256)
    last_name = models.CharField(max_length=256)


I want the Admins model to be able to access all user data, and access the Django Administration page. Below is my Admin creation View -

class AdminSignUpView(CreateView):
    model = User
    form_class = AdminSignUpForm
    template_name = 'form1/signup_form.html'

    def get_context_data(self, **kwargs):
        kwargs['user_type'] = 'ADMIN'
        return super().get_context_data(**kwargs)
    def form_valid(self, form):
        user = form.save()

        return redirect('/P/login_page/')

The form for Admins is -

class AdminSignUpForm(UserCreationForm):
    class Meta(UserCreationForm.Meta):
        model = User
    fname = forms.CharField(label = 'First Name', max_length=256)
    lname = forms.CharField(label = 'Last Name', max_length=256)
    @transaction.atomic
    def save(self):
        user = super().save(commit=False)
        user.save()

        user.is_Admin = True
        user.save()
        admin1 = Admins.objects.create(user=user)
        admin1.first_name=(self.cleaned_data.get('fname'))
        admin1.last_name=(self.cleaned_data.get('lname'))
        return user

I haven't been able to figure it out for weeks. Have I done something wrong? I want to be able to edit, create and delete users using the built-in Django Auth UI while also maintaining my custom Auth model, with the four different hierarchies.

Create a custom view to create admin

from django.contrib.auth.models import User

def new(request):
    if request.method == "POST":
            form = RegistrationForm(request.POST)
            if form.is_valid():
                form.save()
                user = User.objects.create_user(
                    username=form.cleaned_data['username'],
                    password=form.cleaned_data['password'],
                    email=form.cleaned_data['email'],
                    first_name=form.cleaned_data['first_name'],
                    last_name=form.cleaned_data['last_name'],
                    is_staff=True,
                    is_active=True,
                    is_admin=True,
                )
            else:
                print (form.errors)
                return HttpResponse("Validation Error")

            return HttpResponse("Success")
        else:

            context = {
                "form" : form,
            }
            return render(request, 'form.html',context)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM