我正在使用rest_framework_social_oauth2
我想在oauth.User中进行最小的更改。搜索由AbstractUser交换AbstractBaseUser并添加PermissionMixin。但没有任何作用。
我的应用>的 models.py :
from django.db import models
from django.contrib.auth.models import AbstractUser, PermissionsMixin, UserManager
class MyUser(AbstractUser, PermissionsMixin):
country_code = models.CharField(max_length=10, blank=True)
objects = UserManager()
我的 settings.py :
AUTH_USER_MODEL ='userTest.MyUser'
如教程所示,没有makemigrations和迁移。
ERRO:
文件“/Users/luisdemarchi/Git/django/.env/lib/python3.5/site-packages/django/db/models/manager.py”,第277行,获取 self.model._meta.swapped,
AttributeError:Manager不可用; 'auth.User'已被替换为'userTest.MyUser'
答案 0 :(得分:0)
我们成功了。我将在这里为那些想要在OAuth2.user中进行改进的人提供一个完整的示例。在我们的例子中,我们自动创建了用户名。
PS:需要创建类AuthManager()
userTest(myApp)> models.py
import uuid as uuid
from django.contrib.auth.models import BaseUserManager, AbstractUser
from django.db import models
from rest_framework import exceptions
class AuthManager(BaseUserManager):
def create_user(self, email, username=None, password=None):
if not email:
raise exceptions.AuthenticationFailed('Users must have an username')
username = email.split("@")[0]
user = self.model(username=username)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, password, username=None):
user = self.create_user(email=email, password=password)
user.is_admin = True
user.save(using=self._db)
return user
class User(AbstractUser):
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
is_test = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
objects = AuthManager()
userTest(myApp)> views.py 强>
from rest_framework import viewsets
from apps.userTest.models import User
from apps.userTest.serializers import UserSerializer
class UserViewSet(viewsets.ReadOnlyModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer