(Fresh Project) 04. Import Data

The first step: copy the file, set the path

The brands and goods to the following path copy folder, and configure the appropriate field to upload

 

 

Step Two: Import GoodsCategory

Independently models module ready to introduce a large amount of "goods.models.GoodsCategory" one-time data

Goods imported Why not? Because there are foreign keys Goods of GoodsCategory

Category_data.py first source file and to copy product_data.py following path, and then manually create import_category_data import_product_data

 import_category_data.py

# 独立使用django的model模块
import sys,os

# from users.models import UserProfile
# 如果在配置DJANGO_SETTINGS_MODULE之前导入了与django有关的模块,会报错,见下面的截图:
# "You must either define the environment variable DJANGO_SETTINGS_MODULE"

# 把项目根路径加到系统环境变量中去
pwd=os.path.dirname(os.path.abspath(__file__))
sys.path.append(pwd+"../")

# 导入settings文件
os.environ.setdefault("DJANGO_SETTINGS_MODULE","MxShop.settings")

# django初始化
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_instance = GoodsCategory()
lev1_instance.name = lev1_cat["name"]
lev1_instance.code = lev1_cat["code"]
lev1_instance.category_type = 1
lev1_instance.save()
for lev2_cat in lev1_cat["sub_categorys"]:
lev2_instance = GoodsCategory()
lev2_instance.name = lev2_cat["name"]
lev2_instance.code = lev2_cat["code"]
lev2_instance.category_type = 2
lev2_instance.parent_category = lev1_instance
lev2_instance.save()
for lev3_cat in lev2_cat["sub_categorys"]:
lev3_instance = GoodsCategory()
lev3_instance.name = lev3_cat["name"]
lev3_instance.code = lev3_cat["code"]
lev3_instance.category_type = 3
lev3_instance.parent_category = lev2_instance
lev3_instance.save()

 

接下来执行该文件, 就可以发现数据库中自动填充了很多数据, 一共120条, 一级类目有8个

 

 

第三步: 导入Goods

准备独立使用models模块, 来一次性导入

 

 

 

 

 

 

 

 

 

 

-----  over  ------

Guess you like

Origin www.cnblogs.com/jiangzongyou/p/12079209.html