Home > other >  Display index of each item of a sorted list in view Django DRF
Display index of each item of a sorted list in view Django DRF

Time:06-15

I am developing an application with a Django backend and the DRF framework for my api.

At a given url I would like to display the index (the rank) of each user once the list is sorted on my view. I can't explain it very well so I put two examples at the end of my message.

I have not found anything on the internet about how to do this without modifying the template to add a field (I cannot use this solution).

Would you have an idea?

Thanks !

Below my view :


class RankBestPurchaserViewset(viewsets.ViewSet):
    def list(self, request):
        queryset = User.objects.all()
        serializer = RankUserAllPurchaseSerializer(queryset, many=True)
        sortedList = sorted(serializer.data, key=itemgetter(
            'amount'), reverse=True)
        return Response(sortedList)


Below my serializer :

class RankUserAllPurchaseSerializer(serializers.BaseSerializer):
    def to_representation(self, instance):
        return {
            'id': instance.id,
            'username': instance.username,
            'surname': instance.surname,
            'amount': float(SaleProduct.objects.filter(sale__sender__username=instance.username).aggregate(Sum('price'))['price__sum'] or 0),
            'qty_amount': SaleProduct.objects.filter(sale__sender__username=instance.username).count(),
        }

Below the result :


[
    {
        "id": 3,
        "username": "An20",
        "surname": "Khalvin",
        "amount": 426.7,
        "qty_amount": 110
    },
    {
        "id": 1,
        "username": "AA_ENS",
        "surname": "gum",
        "amount": 0.0,
        "qty_amount": 0
    },
    {
        "id": 4,
        "username": "in22",
        "surname": "gum",
        "amount": 0.0,
        "qty_amount": 0
    }
]



desired result


[
    {
        "index":1,
        "id": 3,
        "username": "An20",
        "surname": "Khalvin",
        "amount": 426.7,
        "qty_amount": 110
    },
    {
        "index":2,
        "id": 1,
        "username": "AA_ENS",
        "surname": "gum",
        "amount": 0.0,
        "qty_amount": 0
    },
    {
        "index":3
        "id": 4,
        "username": "in22",
        "surname": "gum",
        "amount": 0.0,
        "qty_amount": 0
    }
]

or

[
    1:{
        "id": 3,
        "username": "An20",
        "surname": "Khalvin",
        "amount": 426.7,
        "qty_amount": 110
    },
    2:{
        "id": 1,
        "username": "AA_ENS",
        "surname": "gum",
        "amount": 0.0,
        "qty_amount": 0
    },
    3:{
        "id": 4,
        "username": "in22",
        "surname": "gum",
        "amount": 0.0,
        "qty_amount": 0
    }
]




CodePudding user response:

Maybe you can add the code in the view.

class RankBestPurchaserViewset(viewsets.ViewSet):
    def list(self, request):
        ...
        sortedList = sorted(serializer.data, key=itemgetter(
            'amount'), reverse=True)
        # here I added the code to add the ranking index 
        for index, sortedItem in enumerate(sortedList):
            sortedList[index].update('index', index   1)
        return Response(sortedList)

CodePudding user response:

Thanks to @David Lu This is the answer :



class RankBestPurchaserViewset(viewsets.ViewSet):
    def list(self, request):
        queryset = User.objects.all()
        serializer = RankUserAllPurchaseSerializer(queryset, many=True)
        sortedList = sorted(serializer.data, key=itemgetter(
            'amount'), reverse=True)
        for index, sortedItem in enumerate(sortedList):
            sortedList[index].update(index=index 1)
        return Response(sortedList)


  • Related