我正在返回order_total, order_number ,quantity and the payment
重复的订单摘要,但我只希望它显示一次。像所有产品一样,应该只在嵌套的串行化器响应中显示,其他产品应该单独显示,而不重复
序列化程式
class OrderSummarySerializer(ModelSerializer):
product = ProductSerializer()
payment = PaymentSerializer()
order_number = SerializerMethodField()
order_total = SerializerMethodField()
class Meta:
model = OrderProducts
fields = ["order_number", "payment", "product", "quantity", "order_total"]
def get_order_total(self, obj):
return obj.order.order_total
def get_order_number(self, obj):
print(obj)
return obj.order.order_number
视图数量
class OrderSummary(APIView):
def get(self, request):
try:
order_number = request.GET.get("order_number")
orders = Orders.objects.get(user=request.user, order_number=order_number)
order_summary = OrderProducts.objects.filter(
user=request.user, order__order_number=order_number, is_ordered=True
)
context = {
"request": request,
}
serializer = OrderSummarySerializer(
order_summary, many=True, context=context
)
return Response(serializer.data, status=status.HTTP_200_OK)
except Exception as e:
print(e)
return Response(
{"error": "Something went wrong"}, status=status.HTTP_400_BAD_REQUEST
我得到的回应#
[
{
"order_number": "ypVtT1",
"payment": {
"payment_method": "cod",
"amount_paid": "",
"status": "Pending",
"created_at": "2022-12-16T16:46:30.915646+05:30"
},
"product": {
"id": 1,
"product_name": "Pendant1",
"sub_product_name": null,
"slug": "pendant-1",
"highest_offer_price": 250.0,
"category_offer_discount": 1250.0,
"product_offer_discount": 2250.0,
"base_price": 2500,
"stock": 5,
"is_available": true,
"product_image": "http://127.0.0.1:8000/media/photos/products/pendant3.webp"
},
"quantity": 1,
"order_total": 2170.0
},
{
"order_number": "ypVtT1",
"payment": {
"payment_method": "cod",
"amount_paid": "",
"status": "Pending",
"created_at": "2022-12-16T16:46:30.915646+05:30"
},
"product": {
"id": 2,
"product_name": "Pendant2",
"sub_product_name": null,
"slug": "pendant-2",
"highest_offer_price": 1750.0,
"category_offer_discount": 1750.0,
"product_offer_discount": null,
"base_price": 3500,
"stock": 11,
"is_available": true,
"product_image": "http://127.0.0.1:8000/media/photos/products/pendant2.webp"
},
"quantity": 1,
"order_total": 2170.0
}
]
我想要的响应:
[
{
"order_number": "ypVtT1",
"quantity": 1,
"order_total": 2170.0
"payment": {
"payment_method": "cod",
"amount_paid": "",
"status": "Pending",
"created_at": "2022-12-16T16:46:30.915646+05:30"
},
"product": {
"id": 1,
"product_name": "Pendant1",
"sub_product_name": null,
"slug": "pendant-1",
"highest_offer_price": 250.0,
"category_offer_discount": 1250.0,
"product_offer_discount": 2250.0,
"base_price": 2500,
"stock": 5,
"is_available": true,
"product_image": "http://127.0.0.1:8000/media/photos/products/pendant3.webp"
},
},
"product": {
"id": 2,
"product_name": "Pendant2",
"sub_product_name": null,
"slug": "pendant-2",
"highest_offer_price": 1750.0,
"category_offer_discount": 1750.0,
"product_offer_discount": null,
"base_price": 3500,
"stock": 11,
"is_available": true,
"product_image": "http://127.0.0.1:8000/media/photos/products/pendant2.webp"
},
}
]
这是因为使用了ModelSerializer
吗?我需要一个Make separate Serialzier来显示分离的东西并在当前的序列化器上调用它吗?
1条答案
按热度按时间3okqufwl1#
您可以创建包含OrderSummary的订单序列化程序,如下所示:
然后在如下视图中使用它: