简体   繁体   English

使用 DRF 的自定义用户身份验证

[英]Custom User Authentication using DRF

Want to create a Web RESTful API with Django.想用 Django 创建一个 Web RESTful API。 For that I'm using Django REST Framework .为此,我使用Django REST Framework

What are the necessary steps to get the authentication using a custom User model (subclassing AbstractBaseUser ) exposing the endpoints to be used?使用自定义 User model (继承AbstractBaseUser )公开要使用的端点获得身份验证的必要步骤是什么?

1. Create a Custom User Model 1.创建自定义用户Model

The documentation is good when it comes to Specifying a custom user model .指定自定义用户 model方面,文档很好。

In our models.py, import AbstractBaseUser and BaseUserManager .在我们的 models.py 中,导入AbstractBaseUserBaseUserManager

Then, create your class, for instances,然后,创建您的 class,例如,

class Profiles(AbstractBaseUser):
    userId = models.CharField(max_length=36, unique= True)
    username = models.CharField(max_lenght=20)
    password = models.CharField(max_lenght=256)
    is_admin = models.BooleanField(default=False)
    is_staff = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)
    ...

Here it's very important to set USERNAME_FIELD and REQUIRED_FIELDS to define which fields are important when logging in.这里设置USERNAME_FIELDREQUIRED_FIELDS来定义登录时哪些字段是重要的非常重要。

Also in your custom user model class, you have to create two functions needed to deal with permissions, namely has_perm() and has_module_perms() .同样在您的自定义用户 model class 中,您必须创建两个处理权限所需的函数,即has_perm()has_module_perms()


2. Create a Custom User Manager 2.创建自定义用户管理器

Now the next step is to create a Custom User Manager , which is something recommended by Django.现在下一步是创建一个自定义用户管理器,这是 Django 推荐的。

So we create a class like所以我们创建一个 class 像

class ProfilesManager(BaseUserManager):
    ...

In that class, one needs to override two methods, namely create_user() and create_superuser() , to define what happens when a user is created and when a superuser is created, respectively.在 class 中,需要覆盖两个方法,即create_user()create_superuser() ,分别定义创建用户和创建超级用户时发生的情况。

Then, we need to tell Profiles class with an objects parameter where this ProfilesManager class is.然后,我们需要用对象参数告诉 Profiles class 这个 ProfilesManager class 在哪里。

class Profiles(AbstractBaseUser):
    userId = models.CharField(max_length=36, unique= True)
    email = models.CharField(max_lenght=20)
    password = models.CharField(max_lenght=256)
    is_admin = models.BooleanField(default=False)
    is_staff = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)
    ...

    USERNAME_FIELDS = 'userId'
    REQUIRED_FIELDS = ['email']

    objects = ProfilesManager()

    def has_perm(self, perm, obj=None):
        return self.is_admin

    def has_module_perms(self, app_label):
        return True

3. Set User Model 3.设置用户Model

Go to your settings.py and add a AUTH_USER_MODEL property, specifying which user model Django should use. Go 到您的 settings.py 并添加一个AUTH_USER_MODEL属性,指定 model Django 应该使用哪个用户。

AUTH_USER_MODEL = 'appName.Profiles'

4. Make migrations 4. 进行迁移

To commit the changes made above one has to make the migrations by running要提交上面所做的更改,必须通过运行进行迁移

python manage.py makemigrations

5. Register User from REST API 5.从REST API注册用户

As we have created the model, then we just need to create the serializers , views and urls.由于我们已经创建了 model,那么我们只需要创建序列化程序、视图和 url。

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

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