扩展用户对象后,如何使Django登录API正常工作? [英] How do I get the Django login api to work after extending the user object?

查看:60
本文介绍了扩展用户对象后,如何使Django登录API正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已按照文档此处扩展我的用户类,但是现在我的登录测试失败.我假设这是由于扩展类未正确链接回处理auth的django视图.

I've followed the documentation here to extend my user class, but now my login tests fail. I'm assuming this is due to the extended class not being linked correctly back to the django view that deals with auth.

r = self.client.post('/login/', {'username': 'test@test.com', 'password': 'test'})

返回

b'\n<!doctype html>\n<html lang="en">\n<head>\n  <title>Not Found</title>\n</head>\n<body>\n  <h1>Not Found</h1><p>The requested resource was not found on this server.</p>\n</body>\n</html>\n'

我在设置中设置了 AUTH_USER_MODEL

AUTH_USER_MODEL = 'api.ApiUser'

我可以在管理员中创建用户,然后照常登录.

I can create users in my admin, and login as normal.

models.py

models.py

class Company(models.Model):
    """
    Represents a company that has access to the same missions/mission plans/etc.
    """
    id = models.UUIDField(primary_key=True, editable=False, default=uuid.uuid4)

    name = models.TextField()
    logo = models.ImageField(blank=True)

    def __str__(self):
        return self.name


class UserManager(BaseUserManager):
    def create_user(self, email, password=None):
        """
        Creates and saves a User with the given email, date of
        birth and password.
        """
        if not email:
            raise ValueError('Users must have an email address')

        user = self.model(
            email=self.normalize_email(email),
        )

        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, email, password=None):
        """
        Creates and saves a superuser with the given email, date of
        birth and password.
        """
        user = self.create_user(
            email,
            password=password,
        )
        user.is_admin = True
        user.save(using=self._db)
        return user


class ApiUser(AbstractBaseUser):
    email = models.EmailField(
        verbose_name='email address',
        max_length=255,
        unique=True,
    )
    company = models.ForeignKey(Company, null=True, blank=True, on_delete=models.CASCADE)
    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)

    objects = UserManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    def __str__(self):
        return self.email

    def has_perm(self, perm, obj=None):
        "Does the user have a specific permission?"
        # Simplest possible answer: Yes, always
        return True

    def has_module_perms(self, app_label):
        "Does the user have permissions to view the app `app_label`?"
        # Simplest possible answer: Yes, always
        return True

    @property
    def is_staff(self):
        "Is the user a member of staff?"
        # Simplest possible answer: All admins are staff
        return self.is_admin

我还需要做些什么才能使身份验证api重新运行?

What else do I need to do to get the authentication api running again?

我需要使用身份验证来实现自己的视图吗?

Do I need to implement my own view using authenticate?

推荐答案

您可以这样做

settings.py

settings.py

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework.authentication.BasicAuthentication',
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework.authentication.TokenAuthentication',
    ]
}

INSTALLED_APPS = ['rest_framework.authtoken']

urls.py

from rest_framework.authtoken import views
urlpatterns += [
    path('api-token-auth/', views.obtain_auth_token)
]

curl -X POST -d"username = username& password = password123"http://localhost:8000/api-token-auth/

curl -X POST -d "username=username&password=password123" http://localhost:8000/api-token-auth/

这篇关于扩展用户对象后,如何使Django登录API正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆