django输出数组或对象列表

时间:2018-05-15 10:01:55

标签: python django django-rest-framework

我正在做我的第一个Django项目而且我被困在这里。我在结果输出中需要两个数组

    1. 待定预订
    1. 批准预订

预订包含用户选择的服务类型。可以有多个预订。

  • status = 0 - 待定
  • status = 1 - 已批准

必需输出

result: {
 "upcoming_pending":    
 [{reservation_id,location_id,location_name,arrival_date,arrival_time,
 departure_date,departure_time,
  services:[{service_id,service_title,service_description},{}],
  add-ons:[{service_id,service_title,service_description},{}],comments}, 
 {},etc]},

  "upcoming_approved":    
 [{reservation_id,location_id,location_name,arrival_date,arrival_time,
 departure_date,departure_time,
  services:[{service_id,service_title,service_description},{}],
  add-ons:[{service_id,service_title,service_description},{}],comments}, 
 {},etc]}

我的代码我正在尝试下面这样的内容

data={}
data["upcoming_pending"]=[]
data["upcoming_approved"]=[]

queryset1 = reservations.objects.filter(user_id=user_id,arrival__gte=datetime.now(),status=0)
for a in queryset1:
  data["upcoming_pending"][a.id]=reservations.objects.filter(id=a.id)
  data["upcoming_pending"][a.id]["services"]=services.objects.filter(service_id__in=ser,type=1)
  data["upcoming_pending"][a.id]["add-ons"]=services.objects.filter(service_id__in=ser,type=2)


queryset2 = reservations.objects.filter(user_id=user_id,arrival__gte=datetime.now(),status=1)
for b in queryset2:
  data["upcoming_approved"][b.id]=reservations.objects.filter(id=a.id)
  data["upcoming_approved"][b.id]["services"]=services.objects.filter(service_id__in=ser,type=1)
  data["upcoming_approved"][b.id]["add-ons"]=services.objects.filter(service_id__in=ser,type=2)

  return Response({"result":data}, status=status.HTTP_200_OK)

1 个答案:

答案 0 :(得分:0)

让我们用Serializer s构建它,自下而上:

  1. 首先,服务的序列化器;
  2. 一个用于预订的序列化程序,带有两个额外字段(服务和附加组件);
  3. 一个包含两个保留组的序列化程序(待定和批准)。
  4. 我假设您的模型名为ServiceReservations

    1。服务

    在Django应用中创建一个serializers.py文件,然后在其中为ModelSerializer模型实例创建Service,如下所示。

    from rest_framework import serializers
    
    class ServiceSerializer(serializers.ModelSerializer):
        class Meta:
            model = Service
    

    2。保留

    对于Reservations模型实例,由于我们要添加两个自定义相关字段,因此我们将创建一个包含我们要显示的所有字段的自定义序列化程序,如下所示。

    class ReservationSerializer(serializers.Serializer):
        # By using 'source' you can rename a field
        reservation_id = serializers.IntegerField(source='pk')
        location_id = serializers.IntegerField()
        location_name = serializers.CharField()
        arrival_date = serializers.DateField()
        arrival_time = serializers.TimeField()
        departure_date = serializers.DateField()
        departure_time = serializers.TimeField()
        # ... other model fields you may need
        # These are nested serializers, 'many' means that they are lists
        services = ServiceSerializer(many=True)
        add_ons = ServiceSerializer(many=True)
    

    请注意:如果在传递给序列化程序的对象中找不到序列化程序字段,序列化将失败。例如。如果departure_date不是Reservation的字段,则上述代码无法正常工作。

    3。待批准

    最后一个序列化器是我们作为响应返回的序列化器:它只需要两个ReservationSerializer作为其字段。

    class PendingApprovedReservationsSerializer(serializers.Serializer):
        upcoming_pending = ReservationSerializer(many=True)
        upcoming_approved = ReservationSerializer(many=True)
    

    全部放在一起

    好的,现在我们已经准备好了所有序列化程序。在我们看来,我们可以做类似的事情:

    pending = reservations.objects.filter(user_id=user_id, 
        arrival__gte=datetime.now(), status=0)
    for a in pending:
      a.services = services.objects.filter(service_id__in=ser, type=1)
      a.add_ons = services.objects.filter(service_id__in=ser, type=2)
    
    approved = reservations.objects.filter(user_id=user_id,
        arrival__gte=datetime.now(), status=1)
    for b in approved:
      b.services = services.objects.filter(service_id__in=ser, type=1)
      b.add_ons = services.objects.filter(service_id__in=ser, type=2)
    
    data = dict(upcoming_pending=pending,upcoming_approved=approved)
    serializer = PendingApprovedReservationsSerializer(data)
    # HTTP 200 status is set by default
    return Response(serializer.data)
    

    免责声明:这是未经测试的代码,它只是一个建议。

    参考DRF nested serializers