Jekson
Jekson

Reputation: 3252

Add new variable to DRF Response

I have a working function, I need to add a new variable to it, the value of which will depend on which part of the code is executed.

working_code.py

class YoutubeAuthView(APIView):    
    permission_classes = (permissions.IsAuthenticated,)    
    def post(self, request, *args, **kwargs):
        .....
        some code
        .....

        try:
           ...some code...
           try:
              p = Platform.objects.get(
                content_type=ContentType.objects.get_for_model(y),
                object_id=y.id,
                user=request.user
            )
           except:
              p = Platform(user=request.user,
                         platform=y, description=description)

        except Youtube.DoesNotExist:
           p = Platform(user=request.user,
                         platform=y, description=description)                
        return Response(
            PlatformSerializer(p, context={'request': request}).data
        )

Now I add variable NEW

class MyAuthView(APIView):    
    permission_classes = (permissions.IsAuthenticated,)    
    def post(self, request, *args, **kwargs):
        .....
        some code
        .....

        try:
           ...some code...
           try:
              p = Platform.objects.get(
                content_type=ContentType.objects.get_for_model(y),
                object_id=y.id,
                user=request.user
            )
           except:
              p = Platform(user=request.user,
                         platform=y, description=description)
           NEW = False
        except Youtube.DoesNotExist:
           p = Platform(user=request.user,
                         platform=y, description=description)
           NEW = True                
        return Response(?????)

How to add right in return Response variable NEW? Something like PlatformSerializer(p, context={'request': request, 'new':new}).data

Upvotes: 1

Views: 523

Answers (2)

Ehsan Nouri
Ehsan Nouri

Reputation: 2040

I think a better approach is to assign this value to the object before giving it to the serializer, like this for example

    ...
    try:
       ...some code...
       try:
          p = Platform.objects.get(
            content_type=ContentType.objects.get_for_model(y),
            object_id=y.id,
            user=request.user
        )
       except:
          p = Platform(user=request.user,
                     platform=y, description=description)
          p.new = False      ######## here

    except Youtube.DoesNotExist:
       p = Platform(user=request.user,
                     platform=y, description=description)
       p.new = True      ######## here        
    return Response(
        PlatformSerializer(p, context={'request': request}).data
    )

and then in your serializer use a SerializerMethodField:

new = serializers.SerializerMethodField()

def get_new(self, obj):
    value = getattr(obj, 'new', False)
    return value

Upvotes: 1

neverwalkaloner
neverwalkaloner

Reputation: 47354

You can override serializer's to_representation method for this:

PlatformSerializer(ModelSerializer):
    ...
    def to_representation(self, obj):
        data = super().to_representation(obj)
        data['new'] = self.context.get('new')
        return data 

In thew view:

serializer = PlatformSerializer(p, context={'request': request, 'new':new})
return Response(serializer.data)

Upvotes: 0

Related Questions