我正在做我的第一个Django项目而且我被困在这里。我在结果输出中需要两个数组
预订包含用户选择的服务类型。可以有多个预订。
必需输出
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)
答案 0 :(得分:0)
让我们用Serializer
s构建它,自下而上:
我假设您的模型名为Service
和Reservations
。
在Django应用中创建一个serializers.py
文件,然后在其中为ModelSerializer
模型实例创建Service
,如下所示。
from rest_framework import serializers
class ServiceSerializer(serializers.ModelSerializer):
class Meta:
model = Service
对于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
的字段,则上述代码无法正常工作。
最后一个序列化器是我们作为响应返回的序列化器:它只需要两个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)
免责声明:这是未经测试的代码,它只是一个建议。