Beispiel #1
0
def import_category_data():
    from goods.models import GoodsCategory

    with open('data/category_data.json', 'r', encoding='utf8') as f:
        row_data = json.loads(f.read())

    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()
    def addcate(self):
        # 一级分类
        for lev1_cat in book_categories:
            lev1_intance = GoodsCategory()
            lev1_intance.code = lev1_cat["cate_code"]
            lev1_intance.name = lev1_cat["cate_name"]
            lev1_intance.category_type = 1
            lev1_intance.save()

            # 二级分类
            for lev2_cat in lev1_cat["sub_cate"]:
                lev2_intance = GoodsCategory()
                lev2_intance.code = lev2_cat["cate_code"]
                lev2_intance.name = lev2_cat["cate_name"]
                lev2_intance.category_type = 2
                lev2_intance.parent_category = lev1_intance
                lev2_intance.save()

                # 三级分类
                for lev3_cat in lev2_cat["sub_cate"]:
                    lev3_intance = GoodsCategory()
                    lev3_intance.code = lev3_cat["cate_code"]
                    lev3_intance.name = lev3_cat["cate_name"]
                    lev3_intance.category_type = 3
                    lev3_intance.parent_category = lev2_intance
                    lev3_intance.save()
def add_cate(parent_cate, parent_cate_instance, deap):
    cates = parent_cate.get("sub_categorys", None)
    if cates:
        for sub_cate in cates:
            cate_instance = GoodsCategory()
            cate_instance.name = sub_cate["name"]
            cate_instance.code = sub_cate["code"]
            cate_instance.parent_category = parent_cate_instance
            cate_instance.category_type = deap + 1
            cate_instance.save()
            add_cate(sub_cate, cate_instance, deap + 1)
    else:
        return
Beispiel #4
0
def import_category(apps, schema_editor):
    for lev1_cat in category_data.row_data:
        lev1_instance = GoodsCategory()
        lev1_instance.code = lev1_cat['code']
        lev1_instance.name = lev1_cat['name']
        lev1_instance.category_type = 1
        lev1_instance.save()

        for lev2_cat in lev1_cat['sub_categorys']:
            lev2_instance = GoodsCategory()
            lev2_instance.code = lev2_cat['code']
            lev2_instance.name = lev2_cat['name']
            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.code = lev3_cat['code']
                lev3_instance.name = lev3_cat['name']
                lev3_instance.category_type = 3
                lev3_instance.parent_category = lev2_instance
                lev3_instance.save()
    def import_category_data(self, datas):
        from goods.models import GoodsCategory
        for data_lv1 in datas:
            goods_category_lv1 = GoodsCategory()
            goods_category_lv1.name = data_lv1['name']
            goods_category_lv1.code = data_lv1['code']
            goods_category_lv1.category_type = 1
            goods_category_lv1.save()

            for data_lv2 in data_lv1['sub_categorys']:
                goods_category_lv2 = GoodsCategory()
                goods_category_lv2.name = data_lv2['name']
                goods_category_lv2.code = data_lv2['code']
                goods_category_lv2.category_type = 2
                goods_category_lv2.parent_category = goods_category_lv1
                goods_category_lv2.save()

                for data_lv3 in data_lv2['sub_categorys']:
                    goods_category_lv3 = GoodsCategory()
                    goods_category_lv3.name = data_lv3['name']
                    goods_category_lv3.code = data_lv3['code']
                    goods_category_lv3.category_type = 3
                    goods_category_lv3.parent_category = goods_category_lv2
                    goods_category_lv3.save()
def deal_category(data, depth, category_pro):
    if not isinstance(data, (list, dict)):
        raise TypeError("数据格式不正确")

    if isinstance(data, dict) and len(data):
        category_instance = GoodsCategory()
        category_instance.code = data["code"]
        category_instance.name = data["name"]
        category_instance.category_type = depth
        if category_pro is not None:
            category_instance.parent_category = category_pro
        category_instance.save()

        next_node = data.get('sub_categorys', '')
        if isinstance(next_node, list):
            for node in next_node:
                deal_category(node, depth + 1, category_instance)
Beispiel #7
0
def add_data(data, type, parent_instance=None):
    '''
    填充数据用的
    :param data: 数据主体
    :param type: 类型
    :param parent_instance: 父类实体
    :return goods 实例主体:
    '''
    goods = GoodsCategory()

    goods.code = data['code']
    goods.name = data['name']
    goods.category_type = type
    if parent_instance is not None:
        goods.parent_category = parent_instance
    goods.save()
    return goods
import os

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

import django
django.setup()

from goods.models import GoodsCategory

from db_tools.data.category_data import row_data

for leve1_cat in row_data:
    leve1_intance = GoodsCategory()
    leve1_intance.code = leve1_cat["code"]
    leve1_intance.name = leve1_cat["name"]
    leve1_intance.category_type = 1
    leve1_intance.save()
    for leve2_cat in leve1_cat["sub_categorys"]:
        leve2_intance = GoodsCategory()
        leve2_intance.code = leve2_cat["code"]
        leve2_intance.name = leve2_cat["name"]
        leve2_intance.category_type = 2
        leve2_intance.parent_category = leve1_intance
        leve2_intance.save()

        for leve3_cat in leve2_cat["sub_categorys"]:
            leve3_intance = GoodsCategory()
            leve3_intance.code = leve3_cat["code"]
            leve3_intance.name = leve3_cat["name"]
import os

pwd = os.path.dirname(os.path.realpath(__file__))
sys.path.append(pwd + "../")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "daydayfresh.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"]
import sys

pwd = os.path.dirname(os.path.realpath(__file__))
sys.path.append(pwd + "../")

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'shopping.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_sintance = GoodsCategory()
    lev1_sintance.code = lev1_cat["code"]
    lev1_sintance.name = lev1_cat["name"]
    lev1_sintance.category_type = 1
    lev1_sintance.save()

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

        for lev3_cat in lev2_cat["sub_categorys"]:
            lev3_sintance = GoodsCategory()
            lev3_sintance.code = lev3_cat["code"]
pwd = os.path.dirname(os.path.relpath(__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_instance = GoodsCategory()   #数据库的对象等于GoodsCategory
    lev1_instance.code = lev1_cat["code"]   #实例的code等于lev1_cat中的code
    lev1_instance.name = lev1_cat["name"]  # 实例的name等于lev1_cat中的name
    lev1_instance.category_type = 1   #实例的类型选择一级目录 “1”
    lev1_instance.save()    #保存到数据库

    #遍历子类
    for lev2_cat in lev1_cat["sub_categorys"]:   #遍历子类
        lev2_instance = GoodsCategory()  # 数据库的对象等于GoodsCategory
        lev2_instance.code = lev2_cat["code"]  # 实例的code等于lev2_cat中的code
        lev2_instance.name = lev2_cat["name"]  # 实例的name等于lev2_cat中的name
        lev2_instance.category_type = 2  # 实例的类型选择二级目录 “2”
        lev2_instance.parent_category = lev1_instance   # lev2的父类是lev1
        lev2_instance.save()  # 保存到数据库

        #遍历lev2的子类
        for lev3_cat in lev2_cat["sub_categorys"]:  # 遍历子类
Beispiel #12
0
import sys
import os

pwd = os.path.dirname(os.path.relpath(__file__))
sys.path.append(os.path.abspath(os.path.join(pwd, '..')))
sys.path.append(pwd)
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Freshshop.settings')
import django
django.setup()

from goods.models import GoodsCategory
from db_tools.data import category_data

for rank1_cate in category_data.row_data:
    goods_cate_1 = GoodsCategory()
    goods_cate_1.code = rank1_cate['code']
    goods_cate_1.name = rank1_cate['name']
    goods_cate_1.category_type = 1
    goods_cate_1.save()

    for rank2_cate in rank1_cate['sub_categorys']:
        goods_cate_2 = GoodsCategory()
        goods_cate_2.code = rank2_cate['code']
        goods_cate_2.name = rank2_cate['name']
        goods_cate_2.category_type = 2
        goods_cate_2.parent_category = goods_cate_1
        goods_cate_2.save()

        for rank3_cate in rank2_cate['sub_categorys']:
            goods_cate_3 = GoodsCategory()
            goods_cate_3.code = rank3_cate['code']
Beispiel #13
0

dir_path = os.path.dirname(os.path.abspath(__file__))
sys.path.append(dir_path)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "gulishop.settings")

import django
django.setup()

from db_tools.data.category_data import row_data
from goods.models import GoodsCategory

for lev1 in row_data:
    cat1 = GoodsCategory()
    cat1.name = lev1['name']
    cat1.code = lev1['code'] if lev1['code'] else ''
    cat1.category_type = 1
    cat1.save()
    for lev2 in lev1['sub_categorys']:
        cat2 = GoodsCategory()
        cat2.name = lev2['name']
        cat2.code = lev2['code'] if lev1['code'] else ''
        cat2.category_type = 2
        cat2.parent_category = cat1
        cat2.save()
        for lev3 in lev2['sub_categorys']:
            cat3 = GoodsCategory()
            cat3.name = lev3['name']
            cat3.code = lev3['code'] if lev1['code'] else ''
            cat3.category_type = 3
            cat3.parent_category = cat2
Beispiel #14
0
import django
import os
import sys

from db_tools.data.category_data import row_data
from goods.models import GoodsCategory

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

django.setup()

for level1 in row_data:
    level1_instance = GoodsCategory()
    level1_instance.code = level1['code']
    level1_instance.name = level1['name']
    level1_instance.category_type = 1
    level1_instance.save()

    for level2 in level1['sub_categorys']:
        level2_instance = GoodsCategory()
        level2_instance.code = level2['code']
        level2_instance.name = level2['name']
        level2_instance.category_type = 2
        level2_instance.parent_category = level1_instance
        level2_instance.save()

        for level3 in level2['sub_categorys']:
            level3_instance = GoodsCategory()
            level3_instance.code = level3['code']
Beispiel #15
0
#导入django设置
import django
django.setup()

#这行代码不能卸载前面,一定要写在django.setup()的后面
from goods.models import GoodsCategory
from db_tools.data.category_data import row_data
print(row_data)

# all_GoodsCategory = GoodsCategory.objects.all()
# print(all_GoodsCategory)
#一级类目的数据
for level1_data in row_data:
	level1_instance = GoodsCategory()
	level1_instance.name = level1_data["name"]
	level1_instance.code = level1_data["code"]
	level1_instance.category_type = 1#类别属于一级类目
	#保存数据
	level1_instance.save()
	#二级类目的数据
	for level2_data in level1_data["sub_categorys"]:
		level2_instance = GoodsCategory()
		level2_instance.name = level2_data["name"]
		level2_instance.code = level2_data["code"]
		level2_instance.category_type = 2  # 类别属于二级类目
		#这个不要忘记
		level2_instance.parent_category = level1_instance
		# 保存数据
		level2_instance.save()

		#三级类目的数据
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

import_data = row_data

for data_1 in import_data:
    obj_1 = GoodsCategory()
    obj_1.name = data_1["name"]
    obj_1.code = data_1["code"]
    obj_1.category_type = 1
    obj_1.save()

    for data_2 in data_1["sub_categorys"]:
        obj_2 = GoodsCategory()
        obj_2.name = data_2["name"]
        obj_2.code = data_2["code"]
        obj_2.category_type = 2
        obj_2.parent_category = obj_1
        obj_2.save()

        for data_3 in data_2["sub_categorys"]:
            obj_3 = GoodsCategory()
            obj_3.name = data_2["name"]
            obj_3.code = data_2["code"]
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_data in row_data:
    lev1_instance = GoodsCategory()
    lev1_instance.name = lev1_data["name"]
    lev1_instance.code = lev1_data["code"]
    lev1_instance.category_type = 1
    lev1_instance.save()

    for lev2_data in lev1_data["sub_categorys"]:
        lev2_instance = GoodsCategory()
        lev2_instance.name = lev2_data["name"]
        lev2_instance.code = lev2_data["code"]
        lev2_instance.parent_category = lev1_instance
        lev2_instance.category_type = 2
        lev2_instance.save()

        for lev3_data in lev2_data["sub_categorys"]:
            lev3_instance = GoodsCategory()
            lev3_instance.name = lev3_data["name"]
            lev3_instance.code = lev3_data["code"]
#设置model运行的独立环境
#这是在manage.py文件里面的路径
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Shop.settings")

import django
django.setup()

from goods.models import GoodsCategory
from db_tools.data.category_data import row_data
# all_GoodsCategory = GoodsCategory.objects.all()

#把数据存入数据库中
for level1_data in row_data:
	level1_instance = GoodsCategory()
	level1_instance.name = level1_data['name']
	level1_instance.code = level1_data['code']
	level1_instance.category_type = 1
	level1_instance.save()

	for level2_data in level1_data['sub_categorys']:
		level2_instance = GoodsCategory()
		level2_instance.name = level2_data['name']
		level2_instance.code = level2_data['code']
		level2_instance.category_type = 2
		level2_instance.parent_category = level1_instance
		level2_instance.save()

		for level3_data in level2_data['sub_categorys']:
			level3_instance = GoodsCategory()
			level3_instance.name = level3_data['name']
			level3_instance.code = level3_data['code']
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 lvl1_cat in row_data:
    lvl1_instance = GoodsCategory()
    lvl1_instance.code = lvl1_cat["code"]
    lvl1_instance.name = lvl1_cat["name"]
    lvl1_instance.category_type = 1
    lvl1_instance.save()

    for lvl2_cat in lvl1_cat["sub_categorys"]:
        lvl2_instance = GoodsCategory()
        lvl2_instance.code = lvl2_cat["code"]
        lvl2_instance.name = lvl2_cat["name"]
        lvl2_instance.category_type = 2
        lvl2_instance.parent_category = lvl1_instance
        lvl2_instance.save()

        for lvl3_cat in lvl2_cat["sub_categorys"]:
            lvl3_instance = GoodsCategory()
            lvl3_instance.code = lvl3_cat["code"]
Beispiel #20
0
import os
import django

pwd=os.path.dirname(os.path.realpath(__file__))
sys.path.append(pwd+"../")

#django初始化
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Shop.settings")
django.setup()

from goods.models import GoodsCategory#必须在django初始化之后才能引入
from db_tools.data.category_data import row_data

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

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

        for lev3_cat in lev2_cat["sub_categorys"]:
            lev3_obj = GoodsCategory()
            lev3_obj.code = lev3_cat["code"]
sys.path.append(pwd + "../")
# 要想单独使用django的model,必须指定一个环境变量,会去settings配置找
# 参照manage.py里面就知道为什么这样设置了
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_instance = GoodsCategory()
    lev1_instance.code = lev1_cat['code']
    lev1_instance.name = lev1_cat['name']
    lev1_instance.category_type = 1
    # 保存到数据库
    lev1_instance.save()

    # 二级类
    for lev2_cat in lev1_cat['sub_categorys']:
        lev2_instance = GoodsCategory()
        lev2_instance.code = lev2_cat['code']
        lev2_instance.name = lev2_cat['name']
        lev2_instance.category_type = 2
        lev2_instance.parent_category = lev1_instance
        lev2_instance.save()

        # 三级类
Beispiel #22
0
import django

from goods.models import GoodsCategory

from db_tools.data.category_data import row_data

props = {
    'code': 'code',
    'name': 'name',
    'sub_categories': 'sub_categories'
}

for leve1_cat in row_data:
    leve1_instance = GoodsCategory()
    leve1_instance.code = leve1_cat[props['code']]
    leve1_instance.name = leve1_cat[props['name']]
    leve1_instance.category_type = 1
    leve1_instance.save()

    for leve2_cat in leve1_cat[props['sub_categories']]:
        leve2_instance = GoodsCategory()
        leve2_instance.code = leve2_cat[props['code']]
        leve2_instance.name = leve2_cat[props['name']]
        leve2_instance.category_type = 2
        leve2_instance.parent_category = leve1_instance
        leve2_instance.save()

        for leve3_cat in leve2_cat[props['sub_categories']]:
            leve3_instance = GoodsCategory()
            leve3_instance.code = leve3_cat[props['code']]
sys.path.append(pwd + "../")  # 当前的项目目录,加到根搜索路径
# 设计配置文件的 环境变量, 上面设置了项目根目录
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "binshop.settings")

import django

django.setup()

from goods.models import GoodsCategory
from db_tools.data.category_data import row_data

# 顶级类
top = GoodsCategory()
top.id = 1
top.name = '顶级类'
top.code = '000'
top.category_type = 0
top.parent_category_id = 1
top.save()

# 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"]
from goods.models import GoodsCategory

#实验:没有模拟pyhton正式model环境和导入django设置,请求下保存数据,是否可行---?不可行
# from users.models import VerifyCode
#
# verifycode = VerifyCode()
# verifycode.code = "1234"
# verifycode.mobile = "18601042258"
# verifycode.save()

#存数据,--->数据库
for item1 in row_data:
    instance1 = GoodsCategory()
    instance1.category_type = 1  #当前就是一级商品类目
    instance1.name = item1["name"]  #商品类别的名称
    instance1.code = item1["code"]  #商品类别的编码
    instance1.save()  #保存数据库

    for item2 in item1["sub_categorys"]:
        instance2 = GoodsCategory()
        instance2.category_type = 2  # 当前就是二级商品类目
        instance2.name = item2["name"]  # 商品类别的名称
        instance2.code = item2["code"]  # 商品类别的编码
        instance2.parent_category = instance1  #上级商品类目
        instance2.save()  #保存到数据库

        for item3 in item2["sub_categorys"]:
            instance3 = GoodsCategory()
            instance3.category_type = 3  # 当前就是二级商品类目
            instance3.name = item3["name"]  # 商品类别的名称
            instance3.code = item3["code"]  # 商品类别的编码
# -*- coding: utf-8 -*-
__author__ = 'XiaoWei'

# 独立使用Django的model
import os
import sys
import django
pwd = os.path.dirname(os.path.realpath(__file__))
# 将db_tools 添加到环境目录
sys.path.append(pwd + '../')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'MxShop.settings')
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']
        int(goods_detail["market_price"].replace("¥", "").replace("元", "")))
    goods.shop_price = float(
        int(goods_detail["sale_price"].replace("¥", "").replace("元", "")))
    goods.goods_brief = goods_detail["desc"] if goods_detail["desc"] else ""
    goods.goods_desc = goods_detail["goods_desc"] if goods_detail[
        "goods_desc"] else ""
    goods.goods_front_image = goods_detail["images"][0] if goods_detail[
        "images"] else ""

    category_name = goods_detail["categorys"][-1]
    # use filter instead of get here to avoid raising exceptions when there are none or more than one result
    category = GoodsCategory.objects.filter(name=category_name)
    if category:
        goods.category = category[0]
    else:
        category = GoodsCategory()
        category.name = category_name
        parent_name = goods_detail["categorys"][-2]
        category.parent_category = GoodsCategory.objects.filter(
            name=parent_name)[0]
        category.code = category_name
        category.category_type = 2
        category.save()
        goods.category = category
    goods.save()

    for goods_image in goods_detail['images']:
        goods_image_instance = GoodsImage()
        goods_image_instance.image = goods_image
        goods_image_instance.goods = goods
        goods_image_instance.save()
Beispiel #27
0
pwd = os.path.dirname(os.path.realpath(__file__))
#获取项目的跟目录
sys.path.append(pwd + "../")
#要想单独使用django的model,必须指定一个环境变量,会去settings配置找
#参照manage.py里面就知道为什么这样设置了
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']
Beispiel #28
0
import os
import django

pwd = os.path.dirname(os.path.realpath(__file__))
sys.path.append(pwd + "../")

#django初始化
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Shop.settings")
django.setup()

from goods.models import GoodsCategory  #必须在django初始化之后才能引入
from db_tools.data.category_data import row_data

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

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

        for lev3_cat in lev2_cat["sub_categorys"]:
            lev3_obj = GoodsCategory()
            lev3_obj.code = lev3_cat["code"]
pwd = os.path.dirname(os.path.abspath(__file__))
sys.path.append(pwd + "../")
# 应用manage.py中的代码:将用到setting中的数据库的配置,因为我们要将category_data中的数据导入数据库
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "FoodMarket.settings")

django.setup()

# 这个必须放在os.environ.setdefault("DJANGO_SETTINGS_MODULE", "FoodMarket.settings")之后
# 因为当还没有把FoodMartket目录添加进来,也还没有把setting文件当做为django默认setting文件
from db_tools.data.category_data import row_data
from goods.models import GoodsCategory

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

    for category2 in category1["sub_categorys"]:
        instance2 = GoodsCategory()
        instance2.name = category2["name"]
        instance2.code = category2["code"]
        instance2.parent_category = instance1
        instance2.category_type = 2
        instance2.save()

        for category3 in category2["sub_categorys"]:
            instance3 = GoodsCategory()
            instance3.name = category3["name"]
            instance3.code = category3["code"]
Beispiel #30
0
import django
django.setup()

from goods.models import GoodsCategory

from db_tools.data.category_data import row_data


def add_cate(parent_cate, parent_cate_instance, deap):
    cates = parent_cate.get("sub_categorys", None)
    if cates:
        for sub_cate in cates:
            cate_instance = GoodsCategory()
            cate_instance.name = sub_cate["name"]
            cate_instance.code = sub_cate["code"]
            cate_instance.parent_category = parent_cate_instance
            cate_instance.category_type = deap + 1
            cate_instance.save()
            add_cate(sub_cate, cate_instance, deap + 1)
    else:
        return


for cate in row_data:
    cate_instance = GoodsCategory()
    cate_instance.name = cate["name"]
    cate_instance.code = cate["code"]
    cate_instance.category_type = 1
    cate_instance.save()
    add_cate(cate, cate_instance, deap=1)
Beispiel #31
0
sys.path.append(pwd + "../")
# manage.py中
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Wj_Shop.settings")

import django
django.setup()

# 这行代码必须在初始化django之后
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"]:
Beispiel #32
0
print(sys.path)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "atguigushop.settings")

import django

django.setup()

from goods.models import GoodsCategory
from db_utils.data.category_data import row_data

# all_GoodsCategory=GoodsCategory.objects.all()
# print(all_GoodsCategory)
for item in row_data:
    instance1=GoodsCategory()
    instance1.name=item['name']
    instance1.code=item['code']
    instance1.category_type=1
    instance1.save()

    for item2 in item["sub_categorys"]:
        instance2=GoodsCategory()
        instance2.name=item2["name"]
        instance2.code=item2["code"]
        instance2.category_type = 2
        instance2.parent_category=instance1

        instance2.save()

        for item3 in item2["sub_categorys"]:
            instance3 = GoodsCategory()
            instance3.name = item3["name"]