用户中心的订单查询
创建序列化器
编辑子应用 orders 目录下的 serializers.py 文件,加入:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
...
class GoodsSKUSerializers(serializers.ModelSerializer): """ SKU 序列化器 """ class Meta: model = SKU fields = ('id', 'name', 'default_image_url', 'price')
class OrderGoodsSKUSerializers(serializers.ModelSerializer): sku = GoodsSKUSerializers(read_only=True) class Meta: model = OrderGoods fields = ('count', 'price', 'sku', 'is_commented')
class OrderInfoGoodsSKUSerializers(serializers.ModelSerializer): """
""" skus = OrderGoodsSKUSerializers(many=True, read_only=True) create_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S') class Meta: model = OrderInfo fields = ('order_id', 'create_time', 'total_amount', 'freight', 'pay_method', 'status', 'skus')
|
创建视图
编辑子应用 orders 目录下的 views.py 文件,添加:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
...
from .serializers import OrderSettlementSerializers, SaveOrderSerializers,OrderInfoGoodsSKUSerializers from . models import OrderInfo
...
class OrderAllView(ListAPIView): """ 我的订单视图 -- 全部订单商品 --- 查询 """ permission_classes = [IsAuthenticated]
serializer_class = OrderInfoGoodsSKUSerializers
filter_backends = (OrderingFilter,) ordering_fields = ('create_time',)
def get_queryset(self): orders = OrderInfo.objects.filter(user=self.request.user).order_by('-create_time')
return orders
|
添加访问路由
编辑子应用 orders 目录下的 urls.py 文件,添加“
1 2 3 4 5 6 7 8 9 10
|
from django.urls import path, re_path from . import views
urlpatterns = [ path('orders/settlement/', views.OrderSettlementView.as_view()), path('orders/', views.SaveOrderView.as_view()), path('orders/all/', views.OrderAllView.as_view()) ]
|