Search code examples
djangodjango-rest-frameworkdjango-viewsdjango-serializerdjango-jsonfield

Sorting the django objects based on SerializerMethodField


I am trying to order the user profiles based on the timestamp of the last message in between both the users.

I am using SerializerMethodField to get the timestamp of the last message.

is there any way I can sort the data?

class UserProfileSerializer(serializers.ModelSerializer):
    lastmessage = serializers.SerializerMethodField()
    class Meta:
        model = User
        fields = ['id','lastmessage']
    def get_lastmessage(self,obj): 
        k = self.context.get('view').kwargs['sid']
        data =( Message.objects.filter(receiver=obj.id,sender=k) | Message.objects.filter(sender=obj.id,receiver=k)).order_by('-timestamp').values('message','timestamp')
        if len(data) == 0:
            return ""
        else:
            data = data.first()
            data["timestamp"] = str(data["timestamp"])
        return str(data)

My view:

   class UserChatViewSet(viewsets.ModelViewSet): 
       queryset = User.objects.all() 
       serializer_class = UserProfileSerializer

Now my views return:

[{
    "id": 4,
    "lastmessage": "{'message': 'random', 'timestamp': '2020-06-14 23:49:33.077749+00:00'}"
},
{
    "id": 5,
    "lastmessage": ""
},
{
    "id": 6,
    "lastmessage": "{'message': 'sample', 'timestamp': '2020-06-14 11:53:03.880833+00:00'}"
},
{
    "id": 7,
    "lastmessage": ""
}]

But I want it to sort based on the timestamp of last message


Solution

  • You can overwrite list in order to achieve this:

    def list(self, request, *args, **kwargs):
        response = super().list(request, args, kwargs)
        # sort response.data['results']
        return response
    

    Also, lastmessage can be a dict instead of a str, so it's easier to work with.