广告小程序后端开发(7.获取价格表数据)

1.在xadmin后台,手动添加数据:

2.在apps/trade/目录下新建序列化文件serializers.py:

from rest_framework import serializers
from .models import Price,Order,ShoppingCar


class PriceModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = Price
        fields="__all__"


class OrderModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = Order
        fields="__all__"
        
        
class ShoppingCarModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = ShoppingCar
        fields="__all__"

3.在apps/trade/views.py中编写获取价格表数据的视图类:

from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.renderers import JSONRenderer,BrowsableAPIRenderer
from .serializers import PriceModelSerializer,OrderModelSerializer,ShoppingCarModelSerializer
from .models import Price,Order,ShoppingCar

# Create your views here.


class GetPrice(APIView):
    """
    获取价格表数据
    """
    renderer_classes = [JSONRenderer]  # 渲染器

    def get(self,request):
        price_list = Price.objects.all()
        re = PriceModelSerializer(price_list, many=True)
        return Response(re.data)

4.在apps/trade/目录下新建urls.py:

from django.urls import path
from .views import GetPrice

urlpatterns = [
    path('getprice/',GetPrice.as_view())
]

5.gg/urls.py中:

from django.urls import path,include
from django.views.static import serve
from gg.settings import MEDIA_ROOT

import xadmin

urlpatterns = [
    #path('admin/', admin.site.urls),
    path('media/<path:path>',serve,{'document_root':MEDIA_ROOT}),
    path('xadmin/', xadmin.site.urls),
    path('users/',include('users.urls')),
    path('ad/',include('ad.urls')),
    path('trade/',include('trade.urls'))
]

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券