简体   繁体   中英

Custom DRF list function not returning some of the dict key/value pairs

I'm trying to replace the standard list function inside a Django REST Framework viewset with a custom one, but am losing the count, previous, next params. With no override, a list function returns the following:

{
    "count": 3,
    "next": null,
    "previous": null,
    "results": [...]
}

Whereas when I override the function, I only get the results list, not the higher-level dict with count, next, previous, etc.

What part of the function below is missing from here that would enable that functionality?

def list(self, request):
    loc = Location.objects.filter(user=request.user).latest('timestamp')
    nearby = loc.get_near()  # returns a list with dicts that contain user and dist

    nearby_users = [n['user'] for n in nearby]  # gets a list of users

    serializer = self.get_serializer(nearby_users, many=True)

    return Response(serializer.data)

You can add pagination

def list(self, request):
    loc = Location.objects.filter(user=request.user).latest('timestamp')
    nearby = loc.get_near()  # returns a list with dicts that contain user and dist

    nearby_users = [n['user'] for n in nearby]  # gets a list of users

    # start pagination
    page = self.paginate_queryset(nearby_users)
    if page is not None:
        serializer = self.get_serializer(page, many=True)
        return self.get_paginated_response(serializer.data)
    # end pagination

    serializer = self.get_serializer(nearby_users, many=True)

    return Response(serializer.data)

more details in super mixins from line 39

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM