如何在 Django 休息框架中检查用户名是否存在两个不同的 table

how to check username exit or not two different table in djnago rest framework

在 Tempdriver 中注册新用户时 table 需要验证用户名是否已存在于 tempdriver table 和 appuser table 中,如果检查 tempdriver table 用户名正常,但如果我检查 appuser table 出错,请有人帮助我。

models.py- 添加模型 Tempdriver 和 Appuser

class Tempdriver(BaseModel):
    name = models.CharField(max_length=255)
    username = models.CharField(max_length=20,unique=True,null=True, blank=True)
    mobile = models.CharField(max_length=20,unique=True,null=True, blank=True)
    password = models.CharField(max_length=100)

class AppUser(BaseModel):
    username = models.CharField(max_length=50)

seriliazers.py

class TempDriverUserSerializer(serializers.ModelSerializer):
    class Meta:
        model=Tempdriver
        fields=('username','mobile')
    def validate(self,username):       
        user=Tempdriver.objects.filter(username=username)
        user_exists = AppUser.objects.filter(username=username)
        if  user.exists() and user_exists.exists():
            raise serializers.ValidationError("UserName already exists")
        
    def validate_mobile(self, mobile):
        mobile=Tempdriver.objects.filter(mobile=mobile)
        if mobile.exists():
            raise serializers.ValidationError("Mobile Number already exists")

views.py

class Tempuserapi(APIView):
 
    parser_classes=(MultiPartParser,FormParser)
    def post(self, request, format=None):
        serializer = TempDriverUserSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=HTTP_200_OK)
        else:
            return Response(serializer.errors, status= HTTP_200_OK)

跟踪报告

ERROR Internal Server Error: /api/tempusercheck/
Traceback (most recent call last):
  File "E:dec_everestfleet\everest_jarvis\env\lib\site-packages\django\core\handlers\exception.py", line 34, in inner   
    response = get_response(request)
  File "E:dec_everestfleet\everest_jarvis\env\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "E:dec_everestfleet\everest_jarvis\env\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "E:dec_everestfleet\everest_jarvis\env\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "E:dec_everestfleet\everest_jarvis\env\lib\site-packages\django\views\generic\base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "E:dec_everestfleet\everest_jarvis\env\lib\site-packages\rest_framework\views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "E:dec_everestfleet\everest_jarvis\env\lib\site-packages\rest_framework\views.py", line 469, in handle_exception 
    self.raise_uncaught_exception(exc)
  File "E:dec_everestfleet\everest_jarvis\env\lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
    raise exc
  File "E:dec_everestfleet\everest_jarvis\env\lib\site-packages\rest_framework\views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "E:dec_everestfleet\everest_jarvis\api\views.py", line 632, in post
    if serializer.is_valid():
  File "E:dec_everestfleet\everest_jarvis\env\lib\site-packages\rest_framework\serializers.py", line 220, in is_valid   
    self._validated_data = self.run_validation(self.initial_data)
  File "E:dec_everestfleet\everest_jarvis\env\lib\site-packages\rest_framework\serializers.py", line 423, in run_validation
    assert value is not None, '.validate() should return the validated data'
AssertionError: .validate() should return the validated data
ERROR "POST /api/tempusercheck/ HTTP/1.1" 500 20285

您应该return 错误所说的经过验证的数据。

class TempDriverUserSerializer(serializers.ModelSerializer):
    class Meta:
        model=Tempdriver
        fields=('username','mobile')

    def validate_username(self,username):       
        user=Tempdriver.objects.filter(username=username)
        user_exists = AppUser.objects.filter(username=username)
        if  user.exists() and user_exists.exists():
            raise serializers.ValidationError("UserName already exists")
        return username

    def validate_mobile(self, mobile):
        mobile=Tempdriver.objects.filter(mobile=mobile)
        if mobile.exists():
            raise serializers.ValidationError("Mobile Number already exists")
        return mobile

您没有return验证任何内容。将您的验证更改为

def validate(self,attrs):
    username = attrs.get('username')
    user=Tempdriver.objects.filter(username=username)
    user_exists = AppUser.objects.filter(username=username)
    if  user.exists() and user_exists.exists():
        raise serializers.ValidationError("UserName already exists")
    return attrs

还有return你的validate_mobile手机

def validate_username(self,value):
        data = self.get_initial()
        username = data.get('username')
        tempdriver = Tempdriver.objects.filter(username=username).exists()
        appuser = AppUser.objects.filter(username=username).exists()
        if tempdriver :
            raise serializers.ValidationError("Username already exists")
        if appuser:
            raise serializers.ValidationError("Username already exists")
        return value