如何在用户通过验证并具有登录权限django时将用户添加到组中
How to add user to a group when he is verified and have log in rights django
我创建了一个用户,并希望通过默认查看器将他添加到组中,但前提是他验证了他的电子邮件 ID。
我使用 djoser 创建 API 来创建用户。在 post 上,电子邮件已验证。现在我不明白如何在验证电子邮件时实现添加到组。
这是model.py
from django.db import models
from django.contrib.auth.models import AbstractUser, Group
class User(AbstractUser):
# GROUP_CHOICES = (
#('admin','ADMIN'),
#('creator', 'CREATOR'),
#('reader','READER')
#)
#group = models.CharField(max_length=10, choices=GROUP_CHOICES, default='CREATOR')
email = models.EmailField(verbose_name='email',max_length=233,unique=True)
phone = models.CharField(null=True,max_length=255)
is_active=models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
REQUIRED_FIELDS=['username','phone','first_name', 'last_name']
USERNAME_FIELD = 'email'
def get_username(self):
return self.email
#def add_group(self):
# user= User.OneToOneField(User)
# group = Group.objects.get(name='Creator')
# my_group.user_set.add(your_user)
serializer.py
class UserCreateSerializer(UserCreateSerializer):
class Meta(UserCreateSerializer.Meta):
model= User
fields = ('id' ,'email', 'username' ,'password', 'first_name', 'last_name', 'phone')
urls.py 在应用中
urlpatterns = [
path('', include('djoser.urls')),
path('', include('djoser.urls.authtoken')),
]
我已经提到
stack overflow link
但不能将它与我的代码或如何添加它联系起来,如果它是正确的方法。
覆盖 djoser.serializers.ActivationSerializer
的一种可能方法如下 -
from django.contrib.auth.models import Group
from djoser.serializers import ActivationSerializer
class MyActivationSerializer(ActivationSerializer):
def validate(self, attrs):
attrs = super(MyActivationSerializer, self).validate(attrs)
group = Group.objects.get(name='your_group_name')
self.user.groups.add(group)
return attrs
然后在您的 settings.py
中更新以下内容 -
DJOSER = {
# other djoser settings
'SERIALIZERS': {
#other serializers
'activation': 'your_app_name.serializers.MyActivationSerializer',
#other serializers
}
}
我创建了一个用户,并希望通过默认查看器将他添加到组中,但前提是他验证了他的电子邮件 ID。 我使用 djoser 创建 API 来创建用户。在 post 上,电子邮件已验证。现在我不明白如何在验证电子邮件时实现添加到组。
这是model.py
from django.db import models
from django.contrib.auth.models import AbstractUser, Group
class User(AbstractUser):
# GROUP_CHOICES = (
#('admin','ADMIN'),
#('creator', 'CREATOR'),
#('reader','READER')
#)
#group = models.CharField(max_length=10, choices=GROUP_CHOICES, default='CREATOR')
email = models.EmailField(verbose_name='email',max_length=233,unique=True)
phone = models.CharField(null=True,max_length=255)
is_active=models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
REQUIRED_FIELDS=['username','phone','first_name', 'last_name']
USERNAME_FIELD = 'email'
def get_username(self):
return self.email
#def add_group(self):
# user= User.OneToOneField(User)
# group = Group.objects.get(name='Creator')
# my_group.user_set.add(your_user)
serializer.py
class UserCreateSerializer(UserCreateSerializer):
class Meta(UserCreateSerializer.Meta):
model= User
fields = ('id' ,'email', 'username' ,'password', 'first_name', 'last_name', 'phone')
urls.py 在应用中
urlpatterns = [
path('', include('djoser.urls')),
path('', include('djoser.urls.authtoken')),
]
我已经提到 stack overflow link 但不能将它与我的代码或如何添加它联系起来,如果它是正确的方法。
覆盖 djoser.serializers.ActivationSerializer
的一种可能方法如下 -
from django.contrib.auth.models import Group
from djoser.serializers import ActivationSerializer
class MyActivationSerializer(ActivationSerializer):
def validate(self, attrs):
attrs = super(MyActivationSerializer, self).validate(attrs)
group = Group.objects.get(name='your_group_name')
self.user.groups.add(group)
return attrs
然后在您的 settings.py
中更新以下内容 -
DJOSER = {
# other djoser settings
'SERIALIZERS': {
#other serializers
'activation': 'your_app_name.serializers.MyActivationSerializer',
#other serializers
}
}