我有两个模型,Appointment 和 EmployeeEvent。我需要从这些模型中获取数据并将结果组合到一个 get api 请求中。

网址.py

url(r'^calenderevents', calender_events)

View .py
@api_view(['GET'])
def calender_events(request):
    queryset1 = Appointment.objects.all()
    queryset2 = EmployeeEvent.objects.all()
    return Response({'Appointments':json.loads(serializers.serialize('json', queryset1)), 'EmployeeEvents': json.loads(serializers.serialize('json', queryset2))})

当我调用 API 时,我得到了结果,但它包含一些不需要的键,如“pk”、“模型”、“字段”等。同样在约会结果中,我需要完整的客户对象而不是客户 ID。有什么方法可以指定 CustomerSerializer 和查询集?

结果得到
{
  "Appointments": [
    {
      "pk": "33a0fffb-326e-4566-bfb4-b146a87a4f3f",
      "model": "appointment.appointment",
      "fields": {
        "customer": "25503315-8bac-4070-87c1-86bf0630c846",
        "status": "Requested",
        "description": "Assigned appointment",
      }
    },
    {
      "pk": "9da806f5-77f1-41e6-a745-7be3f79d6f7a",
      "model": "appointment.appointment",
      "fields": {
        "customer": "25503315-8bac-4070-87c1-86bf0630c846",
        "status": "Requested",
        "description": "Assigned appointment",
      }
    }
  ],
  "EmployeeEvents": [
    {
      "pk": "f76b5de0-1ab8-4ac3-947d-15ba8941d97d",
      "model": "employee_event.employeeevent",
      "fields": {
        "event_name": "New Event",

        "start_date": "2017-02-17",
        "end_date": "2017-02-22"
      }
    },
    {
      "pk": "56f02290-370e-426c-951e-a93c57fde681",
      "model": "employee_event.employeeevent",
      "fields": {
        "event_name": "New Event",
        "start_date": "2017-02-02",
        "end_date": "2017-03-22"
      }
    }
  ]
}

预期结果
{
  "Appointments": [
    {
      "id": "33a0fffb-326e-4566-bfb4-b146a87a4f3f",
      "customer": {
        "id": "25503315-8bac-4070-87c1-86bf0630c846",
        "firstname": "Customre 1",
        "photo_url": "imagepath",
      },
       "status": "Requested",
       "description": "Assigned appointment"
    },
    {
      "id": "9da806f5-77f1-41e6-a745-7be3f79d6f7a",
      "customer": {
        "id": "15ba8941d97d-8bac-4070-87c1-86bf0630c846",
        "firstname": "Customre 2",
        "photo_url": "imagepath",
      },
       "status": "Requested",
       "description": "Assigned appointment"
    },
    }
  ],
  "EmployeeEvents": [
    {
      "id": "f76b5de0-1ab8-4ac3-947d-15ba8941d97d",
      "event_name": "New Event 1",
      "start_date": "2017-02-17",
      "end_date": "2017-02-22"
    },
    {
      "id": "56f02290-370e-426c-951e-a93c57fde681",
      "event_name": "New Event 2”,
      "start_date": "2017-02-17",
      "end_date": "2017-02-22"
    }
  ]
}

最佳答案

您需要编写一个序列化程序来以所需的格式显示数据。阅读优秀的教程以正确指导您。但是如果你想要一个快速的hacky答案,那么做这样的事情:

serializer = AppointmentSerializer(Appointment.objects.all(), many=True)
return Response(serializer.data)

序列化程序如下所示:
class AppointmentSerializer(serializers.ModelSerializer):
    customer = CustomerSerializer(required=False, allow_null=True)

    class Meta:
        model = Appointment
        fields = ('id', 'customer', 'status', 'etc...')
        related_object = 'customer'

class CustomerSerializer(serializers.ModelSerializer):
    class Meta:
        model = Customer
        fields = ('id', 'first_name', 'etc...')

编辑:更新以包含相关对象的示例

关于python - Django 休息框架 : convert queryset to json response,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43048974/

10-10 04:03