Django Not able to retrieve/fetch versatileImage link (Cropped URL) in SerializerMethodField()

class GetUserImageOneSerializer(serializers.ModelSerializer):
    class Meta:
        image_one = VersatileImageFieldSerializer(
        sizes=[
            ('medium_square_crop', 'crop__400x400'),
        ]
        )
        model = UserImage
        fields = ('image_one',)



class ChatUserSerializer(serializers.ModelSerializer):
      chatImage = serializers.SerializerMethodField()
      class Meta:
          model = Chat
          fields = ('roome_name','chat_user_one','chat_user_two','lastUpdated','chatImage')
      def get_chatImage(self, obj):
          image=UserImage.objects.filter().first() //(for Test) 
          serializer = GetUserImageOneSerializer(image)
          return serializer.data

Here I am trying to pass image of the user in chat serializer ('ChatImage') .For better behavior in UI I need square crop image ,so I tried to use nested serializer ('GetUserImageOneSerializer' inside 'ChatUserSerializer'). But I am getting normal URL only . can you help me to find better way to implement my idea :)

OUT PUT :

[
    {
        "roome_name": "BISM1000BISM1000",
        "chat_user_one": 6,
        "chat_user_two": 5,
        "lastUpdated": "2021-12-11T10:29:21.589947Z",
        "chatImage": {
            "image_one": "/media/userimage/team-1.jpg"
        }
    }
]


I got the solution

I made mistake in Image serializer it should look like this

class GetUserImageOneSerializer(serializers.ModelSerializer):
    image_one = VersatileImageFieldSerializer(
        sizes=[
            ('medium_square_crop', 'crop__400x400'),
        ]
        )
    class Meta:
        model = UserImage
        fields = ('image_one',)
Back to Top