后台管理 - 商品管理之SPU管理

1,spu表数据获取

  • 目的:可以将spu表的信息,展示在页面中

  • 操作流程:

    • 1, 子路由(meiduo_admin/urls.py)

      • #4,spu管理
        router = DefaultRouter()
        router.register(r'goods',spu_views.SPUViewSet,base_name="goods")
        urlpatterns += router.urls
        
        
    • 2,类视图(good/spu_views.py)

      • #1,spu管理
        class SPUViewSet(ModelViewSet):
            pagination_class = MyPageNumberPagination
            serializer_class = spu_serializers.SPUSerializer
            queryset = SPU.objects.all()
        
    • 3,序列化器(spu_serializers.py)

      • #1,spu序列化器
        class SPUSerializer(serializers.ModelSerializer):
        
            #1,重写brand
            brand = serializers.StringRelatedField(read_only=True)
            brand_id = serializers.IntegerField()
        
            #2,重写分类
            category1 = serializers.StringRelatedField(read_only=True)
            category1_id = serializers.IntegerField()
        
            category2 = serializers.StringRelatedField(read_only=True)
            category2_id = serializers.IntegerField()
        
            category3 = serializers.StringRelatedField(read_only=True)
            category3_id = serializers.IntegerField()
        
            class Meta:
                model = SPU
                fields = "__all__"
        

2,spu表获取品牌信息

  • 目的:可以在添加spu的时候,获取品牌信息

  • 操作流程:

    • 1, 子路由(meiduo_admin/urls.py)

      • url(r'^goods/brands/simple/$',spu_views.SPUBrandSimpleView.as_view())
        
    • 2,类视图(good/spu_views.py)

      • #2,spu,brand品牌获取
        class SPUBrandSimpleView(ListAPIView):
            serializer_class = spu_serializers.SPUBrandSimpleSerializer
            queryset = Brand.objects.all()
        
    • 3,序列化器(good/spu_serializers.py)

      • #2,spu,brand序列化器
        class SPUBrandSimpleSerializer(serializers.ModelSerializer):
            class Meta:
                model = Brand
                fields = ("id","name")
        
        

3,spu表获取一级分类信息

  • 目的:可以将一级分类,展示在添加spu的页面中

  • 操作流程:

    • 1, 子路由(meiduo_admin/urls.py)

      • url(r'^goods/channel/categories/$',spu_views.SPUCategoryView.as_view()),
        
    • 2,类视图(good/spu_views.py)

      • #3,spu,category分类获取
        class SPUCategoryView(ListAPIView):
            serializer_class = spu_serializers.SPUCategorySerializer
            queryset = GoodsCategory.objects.filter(parent__isnull=True)
        
    • 3,序列化器(good/spu_serializers.py)

      • #3,spu,category序列化器
        class SPUCategorySerializer(serializers.ModelSerializer):
            class Meta:
                model = GoodsCategory
                fields = ("id","name")
        

4,spu表获取二级和三级分类

  • 目的:可以选中父级之后,查询对应的子集数据

  • 操作流程:

    • 1, 子路由(meiduo_admin/urls.py)

      • url(r'^goods/channel/categories/(?P<category_id>\d+)/$',spu_views.SPUSubsCategoryView.as_view()),
        
    • 2,类视图(good/spu_views.py)

      • #4,spu,二级分类,三级分类
        class SPUSubsCategoryView(ListAPIView):
            serializer_class = spu_serializers.SPUCategorySerializer
        
            def get_queryset(self):
                category_id = self.kwargs.get("category_id")
                queryset = GoodsCategory.objects.filter(parent_id=category_id).all()
                return queryset
        

5,调试fdfs上传图片

  • 目的:可以通过终端,调试fdfs上传图片

    • 1, 将client文件添加到utils中

    • 2,终端调试

      • In [1]: from fdfs_client.client import Fdfs_client                                                                                                                 
        
        In [2]: #1,创建对象                                                                                                                                                
        
        In [3]: client = Fdfs_client('/Users/heJing/Desktop/classes2/day19/meiduo14/meiduo_mall/meiduo_mall/utils/fdfs/client.conf')                                       
        
        In [4]: #2,上传图片                                                                                                                                                
        
        In [5]: client.upload_by_filename("/Users/heJing/Desktop/上课资料2/day19/3_资料/测试图片/haha.jpg")                                                                
        getting connection
        <fdfs_client.connection.Connection object at 0x10f8459e8>
        <fdfs_client.fdfs_protol.Tracker_header object at 0x10f845710>
        Out[5]: 
        {'Group name': 'group1',
         'Remote file_id': 'group1/M00/00/03/rBAMhl11zCyAfqwPAACP4XconFQ208.jpg',
         'Status': 'Upload successed.',
         'Local file name': '/Users/heJing/Desktop/上课资料2/day19/3_资料/测试图片/haha.jpg',
         'Uploaded size': '35.00KB',
         'Storage IP': '172.16.12.134'}
        
        

6,spu表上传图片

  • 目的: 可以通过fdfs上传添加spu的图片

  • 操作流程:

    • 1, 子路由(meiduo_admin/urls.py)

      • url(r'^goods/images/$',spu_views.SPUImageUploadView.as_view()),
        
    • 2,类视图(good/spu_views.py)

      • #5,spu,image上传
        class SPUImageUploadView(APIView):
        
            def post(self,request):
                #1,获取参数
                image = request.FILES.get("image")
        
                #2,校验参数
                if not image:
                    return Response(status=400)
        
                #3,数据入库(fdfs)
                #3,1 上传图片
                client = Fdfs_client(settings.BASE_CONFIG)
                result = client.upload_by_buffer(image.read())
        
                #3,2 判断图片是否上传成功
                if result.get("Status") != "Upload successed.":
                    return Response(status=400)
        
                image_url = result.get("Remote file_id")
        
                #4,返回响应
                return Response({
                    "img_url":"%s%s"%(settings.BASE_URL,image_url)
                })
        

7,spu表数据保存,更新,删除

  • 注意点: 由于使用的是视图集完成的SPU的管理,所以已经实现了
上一篇:电商项目的SPU和SKU


下一篇:ORACLE PSU SPU (2015-11-04)