“导入商品分类数据”的版本间的差异

来自CloudWiki
跳转至: 导航搜索
第7行: 第7行:
  
 
==导入数据==
 
==导入数据==
 +
===拷贝数据===
 
在db_tools目录下新建目录data ,
 
在db_tools目录下新建目录data ,
  
第12行: 第13行:
  
 
[[文件:bd20-3-22.png|600px]]
 
[[文件:bd20-3-22.png|600px]]
 +
 +
===导入数据===
 +
在db_tooles目录下 新建文件 import_category_data.py
 +
 +
内容如下:
 +
 +
<nowiki>
 +
#独立使用django的model
 +
import sys
 +
import os
 +
 +
 +
pwd = os.path.dirname(os.path.realpath(__file__))
 +
parent_path = os.path.dirname(pwd)#获取上级目录
 +
sys.path.append(parent_path)#将上级目录添加到环境变量
 +
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()
 +
</nowiki>
 +
 +
python3 import_category_data.py
 +
 +
验证结果:10.0.0.30:8000
 +
 +
[[文件:bd20-3-23.png|600px]]

2020年6月24日 (三) 08:51的版本

导入图片

将案例项目media文件夹下的brands和goods目录 拷贝至新项目相同位置处

Bd20-3-21.png

注意这里位置实际是和goods/models.py中的GoodsCategoryBrand表、Goods表中的图片上传字段相一致的,如果一个改了,另外一处也要改。

导入数据

拷贝数据

在db_tools目录下新建目录data ,

并将原项目的分类和产品数据category_data.py ,product_data.py 导入其中 。

Bd20-3-22.png

导入数据

在db_tooles目录下 新建文件 import_category_data.py

内容如下:

#独立使用django的model
import sys
import os


pwd = os.path.dirname(os.path.realpath(__file__))
parent_path = os.path.dirname(pwd)#获取上级目录
sys.path.append(parent_path)#将上级目录添加到环境变量
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()

python3 import_category_data.py

验证结果:10.0.0.30:8000

Bd20-3-23.png