在注册时无法为新用户生成令牌。
在POSTMAN上进行测试期间,当我向服务器发送POST请求时,用户被注册,但未为该用户生成令牌。
在POSTMAN上进行测试期间,当我向服务器发送POST请求时,用户被注册,但未为该用户生成令牌。
Environment:
Request Method: POST
Request URL: http://localhost:8000/api/users/
Django Version: 3.0
Python Version: 3.8.0
Installed Applications:
['phone_verify',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'rest_auth',
'rest_framework.authtoken',
'findr']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback (most recent call last):
File "C:\Users\UBITEK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\UBITEK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\UBITEK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\UBITEK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "C:\Users\UBITEK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\views\generic\base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\UBITEK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\rest_framework\views.py", line 505, in dispatch
response = self.handle_exception(exc)
File "C:\Users\UBITEK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\rest_framework\views.py", line 465, in handle_exception
self.raise_uncaught_exception(exc)
File "C:\Users\UBITEK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\rest_framework\views.py", line 476, in raise_uncaught_exception
raise exc
File "C:\Users\UBITEK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\rest_framework\views.py", line 502, in dispatch
response = handler(request, *args, **kwargs)
File "C:\Users\UBITEK\Desktop\FinderAPI\findr\views.py", line 133, in post
Token.objects.filter(user=users).delete()
File "C:\Users\UBITEK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\UBITEK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\query.py", line 904, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "C:\Users\UBITEK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\query.py", line 923, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "C:\Users\UBITEK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\sql\query.py", line 1337, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "C:\Users\UBITEK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\sql\query.py", line 1362, in _add_q
child_clause, needed_inner = self.build_filter(
File "C:\Users\UBITEK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\sql\query.py", line 1271, in build_filter
self.check_related_objects(join_info.final_field, value, join_info.opts)
File "C:\Users\UBITEK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\sql\query.py", line 1112, in check_related_objects
self.check_query_object_type(value, opts, field)
File "C:\Users\UBITEK\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\sql\query.py", line 1093, in check_query_object_type
raise ValueError(
Exception Type: ValueError at /api/users/
Exception Value: Cannot query "sams": Must be "User" instance.
models.py
class User(models.Model):
username = models.CharField('username',max_length=60)
phone_number = models.CharField('phone_number',max_length=11,unique=True)
is_admin = models.BooleanField('staff_status',max_length=6, default=False)
date_joined = models.DateTimeField(default=timezone.now)
#key = models.CharField(max_length=100, unique=True, blank=True)
objects = models.Manager()
def __str__(self):
return self.username
views.py
class UserList(APIView):
def get(self,request):
users = User.objects.all()
serializer = UserSerializer(users,many=True)
return Response(serializer.data)
def post(self,request):
serializer = UserSerializer(data=request.data)
if serializer.is_valid():
users = serializer.save()
if users:
Token.objects.filter(user=users).delete()
token = Token.objects.create(user=users)
json = serializer.data
json['token'] = token.key
return Response(json, status= status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
serializers.py
class UserSerializer(serializers.ModelSerializer):
username = serializers.CharField(
validators=[UniqueValidator(queryset=User.objects.all())]
)
phone_number = serializers.CharField(min_length=11, required=True,
validators=[UniqueValidator(queryset=User.objects.all())])
def create(self, validated_data):
user = User.objects.create(username=validated_data['username'],phone_number=validated_data['phone_number'])
return user
class Meta:
model = User
fields = ('id', 'username', 'phone_number')
答案 0 :(得分:1)
问题的解决方案:向创建方法提供request.user
。
Token.objects.create(user=request.user)
从评论中,您将得到IntegrityError at /api/users/ (1062, "Duplicate entry '1' for key 'user_id'")
。由于Token
模型的用户模型为OneToOneField
,因此您不能为同一用户创建重复条目。您已删除令牌,然后再次创建。由于您要为刚创建的用户创建令牌,因此post方法将类似于:
def post(self,request):
serializer = UserSerializer(data=request.data)
if serializer.is_valid():
users = serializer.save()
if users:
Token.objects.filter(user=users).delete()
token = Token.objects.create(user=users)
# you can update the token by: token.key = token.generate_key() and then calling save()
json = serializer.data
json['token'] = token.key
return Response(serializer.data, status= status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
我检查了此代码,它对我有用。 serializer.save()
将返回一个用户实例,您可以将其传递给Token模型的create()
方法。
修改
看完代码后,我可以说他正在尝试创建自定义用户模型,并且AUTH_USER_MODEL
的设置不正确。定制模型名称也为User
。正确设置并更改其他设置(模型中的USERNAME_FIELD
)可以解决此问题。
答案 1 :(得分:0)
欢迎来到SO,@ Samson Adejoro。
正如@Nalin指出您可以使用Token.objects.create(user=request.user)
一样,这里的问题是您试图为已经拥有一个用户的令牌创建令牌。可能是在测试时您已经为该用户创建了令牌。 user
是OnetoOneField
模型中的Token
字段。
来自文档:
一对一关系。从概念上讲,这类似于 ForeignKey的值为unique = True,但关系的“反”面 将直接返回单个对象。
在分配新令牌之前,请清除令牌创建的上一个令牌。
Token.objects.filter(user=request.user).delete()