How to serialize an 'object list' in Django REST Framework

后端 未结 3 750
一个人的身影
一个人的身影 2021-02-13 20:59

I need a serializer for something like this:

{
    \"items\": {
        12: {
            \"name\": \"item 1\"
        },
        66: {
            \"name\": \"i         


        
3条回答
  •  Happy的楠姐
    2021-02-13 21:18

    Serializer is not needed. You can do simpler with Class-base View:

    from rest_framework import views
    from rest_framework.response import Response
    from django.http import JsonResponse
    
    class ListItems(views.APIView):
    
        def get(self, request, format=None):
            items = Item.objects.all().values()
            return return JsonResponse(list(items), safe=False)
    

提交回复
热议问题