本文介绍了Django Auth Token ValueError当分配给用户时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在关注Django指南,并使用接收者分配验证令牌。但是,以下内容将修复错误
I'm currently following the Django guide and using the receiver to assign an auth token. However, the following retuens the error
无法分配< User:User object>:Token.user必须是User 实例。
##PRE CREATE Method
@receiver(post_save, sender=User)
def create_auth_token(sender, instance=None, created=False, **kwargs):
if created:
Token.objects.create(user=instance)
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
即使将 @receiver
替换为不起作用
def perform_create(self, serializer):
user = serializer.save()
Token.objects.create(user=user)
推荐答案
你的进口是什么样的?在django.auth.models.user旁边还有另一个类用户?
What do your imports look like? Is there another class User beside django.auth.models.user?
尝试从django.contrib.auth.models.User继承您的User类:
Try to inherit your User class from django.contrib.auth.models.User:
models.py:
from django.contrib import auth
class User(auth.models.User):
# place for extra fields
这篇关于Django Auth Token ValueError当分配给用户时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!