商品类别数据接口-2

来自CloudWiki
跳转至: 导航搜索

获取所有一级目录

视图层Views.py

app/goods/views.py:

from .models import Goods,GoodsCategory
from .filters import GoodsFilter
from .serializers import GoodsSerializer,CategorySerializer

class CategoryViewset(mixins.ListModelMixin, viewsets.GenericViewSet):
    """
    list:
        商品分类列表数据
    retrieve:
        获取商品分类详情
    """
    queryset = GoodsCategory.objects.filter(category_type=1)
    serializer_class = CategorySerializer

路由urls.py

from goods.views import GoodsListViewSet, CategoryViewset

router.register(r'goods',GoodsListViewSet, base_name='goods')
#配置category的url
router.register(r'categorys', CategoryViewset, base_name="categorys")

效果

http://10.0.0.30:8000/categorys/

Bd20-6-18.png

获取一级、二级、三级目录

app/goods/serilizers.py 中增加:

CategorySerializer3 代表三级目录,

CategorySerializer2 代表二级目录,

通过各自的sub_cat变量层层嵌套。

class CategorySerializer3(serializers.ModelSerializer):
    class Meta:
        model = GoodsCategory
        fields = "__all__"


class CategorySerializer2(serializers.ModelSerializer):
    sub_cat = CategorySerializer3(many=True)
    class Meta:
        model = GoodsCategory
        fields = "__all__"


class CategorySerializer(serializers.ModelSerializer):
    sub_cat = CategorySerializer2(many=True)
    class Meta:
        model = GoodsCategory
        fields = "__all__"

效果

http://10.0.0.30:8000/categorys/

TTP 200 OK
Allow: GET, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

[
    {
        "id": 1,
        "sub_cat": [
            {
                "id": 2,
                "sub_cat": [
                    {
                        "id": 3,
                        "name": "羊肉",
                        "code": "yr",
                        "desc": "",
                        "category_type": 3,
                        "is_tab": false,
                        "add_time": "2020-06-24T16:34:11",
                        "parent_category": 2
                    },
                    {
                        "id": 4,
                        "name": "禽类",
                        "code": "ql",
                        "desc": "",
                        "category_type": 3,
                        "is_tab": false,
                        "add_time": "2020-06-24T16:34:11",
                        "parent_category": 2
                    },
                    {
                        "id": 5,
                        "name": "猪肉",
                        "code": "zr",
                        "desc": "",
                        "category_type": 3,
                        "is_tab": false,
                        "add_time": "2020-06-24T16:34:11",
                        "parent_category": 2
                    },
                    {
                        "id": 6,
                        "name": "牛肉",
                        "code": "nr",
                        "desc": "",
                        "category_type": 3,
                        "is_tab": false,
                        "add_time": "2020-06-24T16:34:11",
                        "parent_category": 2
                    }
                ],
                "name": "精品肉类",
                "code": "jprl",
                "desc": "",
                "category_type": 2,
                "is_tab": false,
                "add_time": "2020-06-24T16:34:11",
                "parent_category": 1
            },
            {
                "id": 7,
                "sub_cat": [
                    {
                        "id": 8,
                        "name": "参鲍",
                        "code": "cb",
                        "desc": "",
                        "category_type": 3,
                        "is_tab": false,
                        "add_time": "2020-06-24T16:34:11",
                        "parent_category": 7
                    },
                    {
                        "id": 9,
                        "name": "鱼",
                        "code": "yu",
                        "desc": "",
                        "category_type": 3,
                        "is_tab": false,
                        "add_time": "2020-06-24T16:34:11",
                        "parent_category": 7
                    },
                    {
                        "id": 10,
                        "name": "虾",
                        "code": "xia",
                        "desc": "",
                        "category_type": 3,
                        "is_tab": false,
                        "add_time": "2020-06-24T16:34:11",
                        "parent_category": 7
                    },
                    {
                        "id": 11,
                        "name": "蟹/贝",
                        "code": "xb",
                        "desc": "",
                        "category_type": 3,
                        "is_tab": false,
                        "add_time": "2020-06-24T16:34:11",
                        "parent_category": 7
                    }
                ],
                "name": "海鲜水产",
                "code": "hxsc",
                "desc": "",
                "category_type": 2,
                "is_tab": false,
                "add_time": "2020-06-24T16:34:11",
                "parent_category": 1
            },

获取某个一级目录的详细信息

Redtful API 获取某个指定对象的详细信息的格式为 GET /zoos/ID

在DRF中 这可以通过CategoryViewset继承mixins.RetrieveModelMixin来实现。

视图层Views.py

app/goods/views.py:

from .models import Goods,GoodsCategory
from .filters import GoodsFilter
from .serializers import GoodsSerializer,CategorySerializer

class CategoryViewset(mixins.ListModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet):
    """
    list:
        商品分类列表数据
    retrieve:
        获取商品分类详情
    """
    queryset = GoodsCategory.objects.filter(category_type=1)
    serializer_class = CategorySerializer

效果验证

http://10.0.0.30:8000/categorys/108/

只返回某个一级目录下的详细信息。

HTTP 200 OK
Allow: GET, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 108,
    "sub_cat": [
        {
            "id": 109,
            "sub_cat": [],
            "name": "菌菇类",
            "code": "菌菇类",
            "desc": "",
            "category_type": 2,
            "is_tab": false,
            "add_time": "2020-06-24T16:34:11",
            "parent_category": 108
        },
        {
            "id": 110,
            "sub_cat": [],
            "name": "腌干海产",
            "code": "腌干海产",
            "desc": "",
            "category_type": 2,
            "is_tab": false,
            "add_time": "2020-06-24T16:34:11",
            "parent_category": 108
        },
        {
            "id": 111,
            "sub_cat": [],
            "name": "汤料",
            "code": "汤料",
            "desc": "",
            "category_type": 2,
            "is_tab": false,
            "add_time": "2020-06-24T16:34:11",
            "parent_category": 108
        },
        {
            "id": 112,
            "sub_cat": [],
            "name": "豆类",
            "code": "豆类",
            "desc": "",
            "category_type": 2,
            "is_tab": false,
            "add_time": "2020-06-24T16:34:11",
            "parent_category": 108
        },
        {
            "id": 113,
            "sub_cat": [],
            "name": "干菜/菜干",
            "code": "干菜/菜干",
            "desc": "",
            "category_type": 2,
            "is_tab": false,
            "add_time": "2020-06-24T16:34:11",
            "parent_category": 108
        },
        {
            "id": 114,
            "sub_cat": [],
            "name": "干果/果干",
            "code": "干果/果干",
            "desc": "",
            "category_type": 2,
            "is_tab": false,
            "add_time": "2020-06-24T16:34:11",
            "parent_category": 108
        },
        {
            "id": 115,
            "sub_cat": [],
            "name": "豆制品",
            "code": "豆制品",
            "desc": "",
            "category_type": 2,
            "is_tab": false,
            "add_time": "2020-06-24T16:34:11",
            "parent_category": 108
        },
        {
            "id": 116,
            "sub_cat": [],
            "name": "腊味",
            "code": "腊味",
            "desc": "",
            "category_type": 2,
            "is_tab": false,
            "add_time": "2020-06-24T16:34:11",
            "parent_category": 108
        }
    ],
    "name": "天然干货",
    "code": "天然干货",
    "desc": "",
    "category_type": 1,
    "is_tab": false,
    "add_time": "2020-06-24T16:34:11",
    "parent_category": null
}