单独使用django model
* encoding:utf-8 *
author = 'sz.yu'
此脚本 独立使用django model
import os
import sys
获取相关路径
pwd = os.path.dirname(os.path.realpath(file))
parent_pwd = os.path.dirname(pwd)
设置环境变量
sys.path.append(parent_pwd)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Smartshop.settings")
import django
django.setup()
一定要在环境配置好后才能导入
from goods.models import GoodsCategory
数据文件
from db_script_tools.data.category_data import row_data
for category_1 in row_data:
temp1 = GoodsCategory()
temp1.name = category_1["name"]
temp1.code = category_1["code"]
temp1.category_type = 1
temp1.save()
for category_2 in category_1["sub_categorys"]:
temp2 = GoodsCategory()
temp2.name = category_2["name"]
temp2.code = category_2["code"]
temp2.category_type = 2
temp2.parent_category = temp1
temp2.save()
for category_3 in category_2["sub_categorys"]:
temp3 = GoodsCategory()
temp3.name = category_3["name"]
temp3.code = category_3["code"]
temp3.category_type = 3
temp3.parent_category = temp2
temp3.save()