gpt4 book ai didi

python - 使用djangorest_framework时基于自定义用户的 token 认证

转载 作者:太空宇宙 更新时间:2023-11-03 15:41:14 25 4
gpt4 key购买 nike

我有注册页面,用户注册他们的数据。问题是我需要在注册用户注册时向他们提供 token ,为此我有用户模型,并且我添加了 rest_framework.authtoken在已安装的应用程序和 settings.py 中的 DEFAULT_PERMSSIONAUTH_USER_MODEL 中,并且当我尝试注册时,我在 User 类中有所有者字段此错误IntegrityError:(1048,“列'owner_id'不能为空”)

View .py

def transregister(request):
if request.POST:
args = {}
args.update(csrf(request))
# try:

if request.method == 'POST':

user = UserForm(request.POST)
username = request.POST['username']
email = request.POST['email']
password = request.POST['password']
confirm_pwd = request.POST['confirm_pwd']
company_name = request.POST['company']

profile_type = request.POST['trainer-company']

trainerprofile = TrainerForm(request.POST)

corporate_profile = CorporateProfileForm(request.POST)
print username


if user.is_valid():
userModel = user.save(commit=False)
userModel.is_user = True
userModel.is_active=True
userModel.save()

模型.py

class User(models.Model):
username = models.CharField(max_length=150)
#username = models.OneToOneField(User, on_delete=models.CASCADE)
email = models.CharField(max_length=250,unique=True)
company = models.CharField(max_length=250)
password = models.CharField(max_length=150)
confirm_pwd = models.CharField(max_length=150)
is_admin = models.BooleanField(default=False)
is_user = models.BooleanField(default=False)
is_active = models.BooleanField(default=False)
device_id = models.CharField(max_length=1000, blank=True, null=True)

# User = get_user_model()
user = settings.AUTH_USER_MODEL
owner = models.ForeignKey('user', related_name='User', on_delete=models.CASCADE)
highlighted = models.TextField(default = '')

objects = UserManager()

#class Meta:
# db_table = u'index_user'


REQUIRED_FIELDS= ('username',)
USERNAME_FIELD = ('email')

def __str__(self):
return self.username

@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_auth_token(sender, instance=None, created=False, **kwargs):
if created:
Token.objects.create(user=instance)

序列化器.py

class UserSerializer(serializers.ModelSerializer):
owner = serializers.PrimaryKeyRelatedField(many=True, queryset=User.objects.all())
#print 'owner'
#owner = serializers.ReadOnlyField(source='owner.username')
class Meta:
model = User
fields = ('id','username','email','company','password','confirm_pwd','is_admin','is_user','is_active','device_id','highlighted','owner')

最佳答案

在 settings.py 中,您需要配置身份验证类以包含 TokenAuthentication。

要在 settings.py 中设置它,您应该使用:

REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.TokenAuthentication',
)
}

现在,由于您还要创建自己的用户模型,因此您需要为引用您的自定义模型的 AUTH_USER_MODEL 设置提供一个值:

AUTH_USER_MODEL = 'myapp.MyUser'

关于python - 使用djangorest_framework时基于自定义用户的 token 认证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42092791/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com