简体   繁体   English

django将用户注册为超级用户

[英]django register user as superuser

I am using a OneToOne relationship to create a custom user like this: 我正在使用OneToOne关系来创建这样的自定义用户:

class Patient(models.Model):
    user = models.OneToOneField(Django_User, on_delete=models.CASCADE)
    models.PositiveSmallIntegerField(choices=USER_TYPE_CHOICES)
    birthday=models.DateField(auto_now=False, null=True, blank=True)
    USERNAME_FIELD='username'

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

    def save(self, *args, **kwargs):
        super(Patient, self).save(*args, **kwargs)

    def set_birthday(self, birthday):
        birthday = self.birthday

Now there have been some additions to my task and i need to give every user is_staff or is_superuser permissions. 现在,我的任务有了一些补充,我需要给每个用户is_staff或is_superuser权限。 I created a form: 我创建了一个表格:

class UserPermissionForm(forms.Form):
permission = [(1, 'Patient'),
(2, 'Lieferant'),
(3, 'Andere'),
(4, 'Mitarbeiter'),
(5, 'Admin')]
Zugriffsrechte = forms.ChoiceField(choices= permission, widget=forms.Select())

And tried setting the permissions in my view - genericform is just the usual django user creation form, form is the form where the birthday is set and permission is the form seen above: 并尝试在我的视图中设置权限-genericform只是通常的django用户创建表单,form是设置生日的表单,权限是上面看到的表单:

def register_user_view(request):
    title="Register User"
    genericform = GenericCreationForm(request.POST or None)
    form=UserRegisterForm(request.POST or None)
    permission = UserPermissionForm(request.POST or None)
    if form.is_valid() * genericform.is_valid() * permission.is_valid():
        genericform.save()
        username = genericform.cleaned_data.get('username')
        raw_password = genericform.cleaned_data.get('password1')
        user_type = int(request.POST.get('Zugriffsrechte'))
        new_user = authenticate(username=username, password=raw_password)
        profile = form.save(commit=False)
        profile.user = new_user
        profile.save()
        is_superuser = False
        is_staff = False
        if user_type > 3:
            is_staff = True
            print('YOU SHALL BE STAFF')
        if user_type > 4:
            is_superuser = True
            print('YOU SHALL BE ADMIN')
        profile.user.is_staff = is_staff
        profile.user.is_superuser = is_superuser
        login(request,profile.user)
        return redirect("/register/done")
    context={
        "form":form,
        "genericform":genericform,
        "permissionform": permission,
        "title":title,
    }
    return render(request,"profile_form.html",context)

My first question is if and how i could set is_staff or is_superuser. 我的第一个问题是,是否以及如何设置is_staff或is_superuser。 The code above causes no errors but the created user is still just a regular user. 上面的代码没有错误,但是创建的用户仍然只是普通用户。 Also i know this can be simplyfied (i am working with other people so at some point everything got a bit unstructured) so question 2 is: what is the best approach to make this code better? 我也知道这很简单(我正在与其他人一起工作,因此在某些时候一切都变得有些非结构化),所以问题2是:使此代码更好的最佳方法是什么? A custom user with a custom user manager? 具有自定义用户管理器的自定义用户? Or is the OneToOne Field still a good option? 还是OneToOne Field仍然是一个不错的选择?

Seems like you are missing on the saving part. 好像您在保存部分上不见了。 Add profile.save() after you are changing it. 更改后添加profile.save() Something like this. 这样的事情。

profile.user.is_staff = is_staff
profile.user.is_superuser = is_superuser
profile.save()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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