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

2
  • can you post your views get_queryset method Commented Jun 30, 2020 at 12:16
  • class UserChatViewSet(viewsets.ModelViewSet): queryset = User.objects.all() serializer_class = UserProfileSerializer Commented Jun 30, 2020 at 12:23

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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
For the "# sort response.data['results']" line, I used the following sorted_data = sorted(response.data, key=lambda k: k["last_message"]["timestamp"], reverse=True) then response.data = sorted_data return response. (Note this only works if there are no null values in timestamp.)
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')

5 Comments

This Is returning: 'Subquery' object has no attribute 'order_by'
Now this gives: This queryset contains a reference to an outer query and may only be used in a subquery.
Same error. and I think it should be .order_by('-lastmessage__timestamp') instead of order_by('-last_message__timestamp')
thats corrected, but error is different @saibhaskar

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.