python 使用Django Rest Framework在Django中自定义用户实现

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 使用Django Rest Framework在Django中自定义用户实现相关的知识,希望对你有一定的参考价值。

INSTALLED_APPS = (
	'django.contrib.auth',
	'django.contrib.contenttypes',
	'django.contrib.sessions',
)

AUTH_USER_MODEL = 'accounts.User'
from django.contrib.auth.models import Group
from rest_framework import serializers
from hello.models import User


class UserSerializer(serializers.ModelSerializer):
    class Meta:
        print "UserSerializer"
        model = User
        fields = ('id','password', 'email','is_active')
        write_only_fields = ('password',)
        read_only_fields = ('id','is_active')
    
    def create(self, validated_data):
        print "create user"
        user = User.objects.create(
            email=validated_data['email'],
        )

        user.set_password(validated_data['password'])
        user.save()

        return user
    


class GroupSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Group
        fields = ('url', 'name')


from django.db import models
from django.contrib.auth.models import AbstractBaseUser
from django.contrib.auth.models import BaseUserManager

class CustomUserManager(BaseUserManager):

    def _create_user(self, email, password,is_superuser, **extra_fields):
        """
        Creates and saves a User with the given email and password.
        """
        now = timezone.now()
        if not email:
            raise ValueError('The given email must be set')
        email = self.normalize_email(email)
        user = self.model(email=email,
                          is_staff=is_staff, is_active=True,
                          is_superuser=is_superuser, last_login=now,
                          date_joined=now, **extra_fields)
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_user(self, email, password=None, **extra_fields):
        return self._create_user(email, password, False, False,
                                 **extra_fields)

    def create_superuser(self, email, password, **extra_fields):
        return self._create_user(email, password, True, True,
                                 **extra_fields)


# Create your models here.
class User(AbstractBaseUser):
        email = models.EmailField(('email address'), max_length=254, unique=True)
        is_active = models.BooleanField(('active'), default=True,
        help_text=('Designates whether this user should be treated as '
                    'active. Unselect this instead of deleting accounts.'))
        USERNAME_FIELD = 'email'
        REQUIRED_FIELDS = []

        objects = CustomUserManager()

以上是关于python 使用Django Rest Framework在Django中自定义用户实现的主要内容,如果未能解决你的问题,请参考以下文章

python django-rest-framework 3.3.3 更新嵌套序列化程序

“未提供身份验证凭据。” Django 和 AWS 应用程序

Python / Django 中的 REST API

Python第二十四课Django rest framework

Python web 开发django rest framwork 的token 登录和原理

Python前后端分离开发Vue+Django REST framework实战