How to convert arraylist into list only

i have response data on the below:

[{ "id": "8943832a-94e4-49e9-944f-022b65c9aa4b", "created": "2022-09-10T04:35:49.173268Z", "updated": "2022-09-10T04:35:49.204274Z", "pick_up_address": "pokhara", "drop_off_address": "kathmandu", "price": "120", "status": "REQUESTED", "email": "admin5@gmail.com", "username": "admin5" }, { "id": "16004d18-e0d0-4e35-b147-bb3234403602", "created": "2022-09-10T04:40:40.238071Z", "updated": "2022-09-10T04:40:40.282818Z", "pick_up_address": "kathmandu", "drop_off_address": "pokhara", "price": "200", "status": "REQUESTED", "email": "admin5@gmail.com", "username": "admin5" } ]

but i need a response like this on the below


{
        "id": "8943832a-94e4-49e9-944f-022b65c9aa4b",
        "created": "2022-09-10T04:35:49.173268Z",
        "updated": "2022-09-10T04:35:49.204274Z",
        "pick_up_address": "pokhara",
        "drop_off_address": "kathmandu",
        "price": "120",
        "status": "REQUESTED",
        "email": "admin5@gmail.com",
        "username": "admin5"
      },
      {
        "id": "16004d18-e0d0-4e35-b147-bb3234403602",
        "created": "2022-09-10T04:40:40.238071Z",
        "updated": "2022-09-10T04:40:40.282818Z",
        "pick_up_address": "kathmandu",
        "drop_off_address": "pokhara",
        "price": "200",
        "status": "REQUESTED",
        "email": "admin5@gmail.com",
        "username": "admin5"
      }

the code of here below serializer.py

class NestedTripSerializer(serializers.ModelSerializer):
    driver = UserSerializer(read_only=True)
    passenger = UserSerializer(read_only=True)



   # custom in nested serializer
    def to_representation(self, instance):
        #pop passenger and driver
        
        
        data = super().to_representation(instance)
        is_list_view = isinstance(instance, list)
        #passenger email and username
        extra_ret = {
            'email': self.context['request'].user.email,
            'username': self.context['request'].user.username,
            

        }
        if is_list_view:
            for item in data:
                item.update(extra_ret)
        else:
            data.update(extra_ret)
        return data

    def get_
        

        
    class Meta:
        model = Trip
        fields = '__all__'
        depth = 1

and the code of views.py below:-

class TripView(viewsets.ReadOnlyModelViewSet):
    lookup_field = 'id'
    lookup_url_kwarg = 'trip_id'
    
    serializer_class = NestedTripSerializer
    permission_classes = (permissions.IsAuthenticated,)





    def get_queryset(self):
        user = self.request.user
        if user.group == 'driver':
            trip = Trip.objects.filter(Q(status='REQUESTED'))
            
            trip = trip.values('id', 'status', 'created', 'updated', 'pick_up_address', 'drop_off_address', 'price')
            
            return list(trip)
                
        

            
        if user.group == 'passenger':
            trip = Trip.objects.filter(Q(status='ACCEPTED')).values('id', 'status', 'created', 'updated', 'pick_up_address', 'drop_off_address', 'price')
            return list(trip)
            
        
        return Trip.objects.none()

    def list(self, request, *args, **kwargs):
        queryset = self.get_queryset()
        serializer = self.get_serializer(queryset, many=True)
        return Response(serializer.data)
 
    
class CreateTripView(generics.CreateAPIView):
    serializer_class = NestedTripSerializer
    permission_classes = (permissions.IsAuthenticated,)

    def perform_create(self, serializer):
        serializer.save(passenger=self.request.user)
        print('create')

    def get_queryset(self):
        user = self.request.user
        
            
        if user.group == 'passenger':
            return Trip.objects.filter(user.group == 'passenger')
        
        return Trip.objects.none()

#convert trip_id

Back to Top