replacing token with a value


i am trying to replace token by checking whether the token is valid and then taking out the details using that token .

{"jwt":"asdahasjkaiubdkjsdjasdajkdjakdon","hostel":"BCJ bhawan","room_no":"300"……}

something like this i will receive

how can i replace that token portion with the value in serializer1

but i am unable to merge them together

here is my

class leaveview(APIView):
    def post(self,request):
        token =['jwt']
        if not token:
            raise AuthenticationFailed('Unauthenticated')
            payload = jwt.decode(token,'secret',algorithms=['HS256'])
        except jwt.ExpiredSignatureError:
            raise AuthenticationFailed('Unauthenticated')

        serializer2 = leaveSerializers(
        return Response(

class leave(models.Model):
    hostel = models.ForeignKey(hostel_manager,on_delete=models.CASCADE)
    room_no = models.CharField(max_length=10)
    where_to = models.CharField(max_length=100)
    reason = models.CharField(max_length=300)
    start_date = models.CharField(max_length = 100,null=True)
    end_date = models.CharField(max_length=100,null=True)
    phone_regex=RegexValidator(regex=r'^\+?1?\d{9,15}$', message="Phone number must be entered in the format: '+9999999999'. Up to 12 digits allowed.")
    phone_number = models.CharField(validators=[phone_regex], max_length=17)

class leaveSerializers(serializers.ModelSerializer):
    class Meta:
        model = leave
        fields = ['id','hostel','room_no','where_to','reason','time_period','phone_number','name','father_name','branch','coer_id']


Two things first, you have two problems in your questions.

  • Firstly you want to replace a token with a value.
  • Secondly you want to merge serializer together.

In watching your code, we assume that you’re using the jwt auth system from DRF.

Therefore you could simply use something as follow to retrieve the user and be sure that the user is authenticated :

from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated

class leaveview(APIView):
    def post(self,request):
        user = request.user

Then you let DRF handling jwt auth without hussle.

For your merging issue, it’s not a right idea to force uniting things from different nature in such way.

You would have to make serialize your data :

serializer1 = UserSerializers(user)
serializer1_data = UserSerializers(user).data
merged_data = {**serializer1_data, **}
return Response(data=merged_data)

Above should be a working example, the ball is on your side to ease your code.

Answered By – em1le

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published