如何从 Django 序列化程序中的 jwt 令牌访问请求的用户对象?

How to access requested user object from jwt token in django serializers?

After user-registration, user wants to post data into Client model ( OnetoOne relationship with User model ). So, I want to access the requested user object inside the serializer-class to create a new row in Client model associated with the requested user.

models.py

class Client(models.Model):
user= models.OneToOneField(User, on_delete=models.CASCADE, null=False, blank=False)
sex = models.CharField(max_length = 6, blank=False, null=False)
location = models.CharField(max_length = 30, null = False, blank = False)  

views.py

class ClientRegister(GenericAPIView):
def post(self, request):  
    user = request.user
    serializer = ClientSerializer(data= request.data)
    if serializer.is_valid():       
        serializer.save()           
        return Response(status= status.HTTP_201_CREATED)
    else:
        return Response(data= 'Invalid Form', status= status.HTTP_400_BAD_REQUEST)

serializers.py

class ClientSerializer(serializers.ModelSerializer):  
class Meta:
    model = Client
    fields = ['sex', 'location']  

def create(self, validated_data):
    sex = validated_data.get('sex')
    location = validated_data.get('location')         

    user =  #------ Want requested user object here ------#
    
    if user.is_client:    
        client = Client(user=user, sex=sex, location=location)
        client.save()
        return client

I have manually added user oject into the data in serializer = CientSerializer(data=request.data). But, It didn't work. Please, tell me how to pass it from views.py or how to access it in serializers.py.

尝试在浏览量中传递带有性别和位置的用户

class ClientRegister(GenericAPIView):
    def post(self, request):  
        data = request.data
        user = request.user
         #add user in key
        data['user']=user
        serializer = ClientSerializer(data=data)
        if serializer.is_valid():       
            serializer.save()           
            return Response(status= status.HTTP_201_CREATED)
        else:
            return Response(data= 'Invalid Form', status= status.HTTP_400_BAD_REQUEST)

序列化器:

class ClientSerializer(serializers.ModelSerializer):  
   class Meta:
      model = Client
      fields = '__all__'

当您在 post 视图方法中执行 serializer.save 时传递用户

def post(self, request):  
    u = request.user
    serializer = ClientSerializer(data= request.data)
    if serializer.is_valid():       
        serializer.save(user=u)           
        return Response(status= status.HTTP_201_CREATED)
    else:
        return Response(data= 'Invalid Form', status= status.HTTP_400_BAD_REQUEST)

或者您可以阅读有关将上下文从视图传递到序列化程序的信息,在这种情况下,您可以传递所需的数据,例如 -

serializer = ClientSerializer(context = {'request':request}, data=request.data)

然后你可以在你的序列化器中有你的请求对象,你可以得到request.user或者直接在上下文中传递它。

views.py

class ClientRegister(GenericAPIView):
  def post(self, request):  
    user = request.user
    serializer = ClientSerializer(context={'user':user}, data= request.data)
    if serializer.is_valid():       
      serializer.save()           
      return Response(status= status.HTTP_201_CREATED)
    else:
      return Response(data= 'Invalid Form', status= status.HTTP_400_BAD_REQUEST)

serializers.py

 class ClientSerializer(serializers.ModelSerializer):  
   class Meta:
     model = Client
     fields = ['sex', 'location']  

   def create(self, validated_data):
     sex = validated_data.get('sex')
     location = validated_data.get('location')         

     user = self.context.get('user')

     if user.is_client:    
       client = Client(user=user, sex=sex, location=location)
       client.save()
       return client