0

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

Mohit Harshan
  • 1,916
  • 1
  • 18
  • 41
saibhaskar
  • 435
  • 6
  • 21

2 Answers2

1

The order of your response should be handled in the view .

  from django.db.models import Subquery, OuterRef
 lm = Message.objects.filter(sender=OuterRef("id"), receiver=self.kwargs['sid']).order_by('-timestamp')   
    data = User.objects.all().annotate(
        lastmessage=Subquery(
                    lm.values('timestamp')[:1]
                )
    ).order_by('-lastmessage__timestamp')
saibhaskar
  • 435
  • 6
  • 21
Mohit Harshan
  • 1,916
  • 1
  • 18
  • 41
1

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.

Horatiu Jeflea
  • 7,256
  • 6
  • 38
  • 67
  • This will work but as you saw in my output there are some empty values for the last message which is causing an error in sorting – saibhaskar Jun 30 '20 at 16:09