class GoodsSerializer(serializers.ModelSerializer):
    #使用ModelSerializer来实现序列化,直接将serializer和model进行映射,在下面直接指定model就可以进行序列化
    category = GoodsCategory()  #这个是将上面的类使用到这个Goods类里面,直接将数据进行覆盖。
    images = GoodsImageSerializer(many=True)

    class Meta:
        model = Goods
        fields = "__all__"
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
Exemple #3
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
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)
Exemple #5
0
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "backend.settings")

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()
Exemple #6
0
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_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()
# -*- 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']
    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 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()
        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()
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)
Exemple #12
0
sys.path.append(pwd + "../")

#要想单独使用django的model,必须指定一个环境变量,会去settings配置找(连接数据库)
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.name = lev1_cat["name"]
    lev1_intance.code = lev1_cat["code"]
    lev1_intance.category_type = 1  # 一级类别
    lev1_intance.save()

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

        # 三级类

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的子类
Exemple #14
0
import sys
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()
pwd = os.path.dirname(os.path.realpath(__file__))
# print(pwd)  # D:\A-python\MxShop\db_tools

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"]:
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()
Exemple #17
0
dirname=os.path.dirname(filename_path)
sys.path.insert(0,dirname)
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"]:
Exemple #18
0
import sys
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()
Exemple #19
0
import django
django.setup()  #不知道这个函数是干嘛的,但setup是安装的意思,或者也是启动的意思

from goods.models import GoodsCategory  #这句一定要放在14、15这两行的下面,不能放在最顶上。因为必须要先把django引入之后才能写这个,不然会报错
from db_tools.data.category_data import row_data  #引入商品类别

#all_categorys = GoodsCategory.objects.all()
#objects是Django帮我们自动生成的管理器对象,通过这个管理器可以实现对数据的查询, objects是models.Manager类的一个对象,如果我们模型类中添加一个models.Manger类或者其子类变量,那么Django就不再帮我们生成默认的objects管理器。
#自定义管理器的应用场景:案例: 调用BookInfo.objects.all()返回没有删除的图书的数据

for lev1_cat in row_data:
    #lev的意思是等级
    #下面是一个嵌套循环,用来写后台中商品类别选项中的排列。类别的数据是从category_data.py这个文件中导入进来的。然后我们再用下面的逻辑把他导入进xadmin后台中。

    lev1_intance = GoodsCategory()
    #intance的意思是维护、维持

    lev1_intance.code = lev1_cat["code"]
    lev1_intance.name = lev1_cat["name"]
    lev1_intance.category_type = 1  #这里的意思是这个等级类目等于1,1在goods.models中。
    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()
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 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()
import sys
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()
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

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()
Exemple #23
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()
Exemple #24
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']
Exemple #25
0
 class Meta:
     model = GoodsCategory()
     fields = '__all__'
import os

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

import django

django.setup()

from goods.models import GoodCategory as 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()
import os, sys


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
Exemple #28
0
# 获取项目的跟目录
sys.path.append(pwd + "../")

# 要想单独使用django的model,必须指定一个环境变量,在manage.py或wsgi.py中找
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ishop.settings')

import django

django.setup()

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

# 1级分类
for level1_cat in row_data:
    category1 = GoodsCategory()
    category1.name_en = level1_cat["code"]
    category1.name = level1_cat["name"]
    category1.category_level = 1
    category1.save()

    # 二级分类
    for level2_cat in level1_cat["sub_categorys"]:
        category2 = GoodsCategory()
        category2.name_en = level2_cat["code"]
        category2.name = level2_cat["name"]
        category2.category_level = 2
        category2.parent_category = category1
        category2.save()

        # 三级分类
# 单独使用django的model 也就是如何配置文件,可以直接连接数据库使用model导入数据

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()
Exemple #30
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()
Exemple #31
0
# 将项目目录加入setting
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()

        # 三级分类
Exemple #32
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()