3-8 导入商品类别数据

拷贝 goods 图片文件夹

修改:使保持一致

然后把brands 也拷贝过来。

修改:

---------------------------------------------

接下来 新建data 文件夹, 把两个文件拖进来

下面写脚本

C:\Users\huang\PycharmProjects\MxShop\db_tools\import_category_data.py

import sys
import os

pwd = os.path.dirname(os.path.realpath(__file__))
sys.path.append(pwd+"../")
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'MxShop.settings')


import django
django.setup()

from goods.models import GoodsCategory

all_categorys = GoodsCategory.objects.all()

单独运行,不报错。

其中

    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'MxShop.settings')

来自:C:\Users\huang\PycharmProjects\MxShop\manage.py

然后运行。

import sys
import os

pwd = os.path.dirname(os.path.realpath(__file__))
sys.path.append(pwd+"../")
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'MxShop.settings')


import django
django.setup()

from goods.models import GoodsCategory

from db_tools.data.category_data import row_data

for lev1_cat in row_data:
    lev1_intance = GoodsCategory()
    lev1_intance.code = lev1_cat["code"]
    lev1_intance.name = lev1_cat["name"]
    lev1_intance.category_type = 1
    lev1_intance.save()

    for lev2_cat in lev1_cat["sub_categorys"]:
        lev2_intance = GoodsCategory()
        lev2_intance.code = lev2_cat["code"]
        lev2_intance.name = lev2_cat["name"]
        lev2_intance.category_type = 2
        lev2_intance.parent_category = lev1_intance
        lev2_intance.save()

        for lev3_cat in lev2_cat["sub_categorys"]:
            lev3_intance = GoodsCategory()
            lev3_intance.code = lev3_cat["code"]
            lev3_intance.name = lev3_cat["name"]
            lev3_intance.category_type = 3
            lev3_intance.parent_category = lev2_intance
            lev3_intance.save()

数据:

猜你喜欢

转载自blog.csdn.net/huanglianggu/article/details/84454008
今日推荐