import django django.setup() from goods.models import Goods, Category, DetailSlide from db_tools.data.product_data import row_data for goods_detail in row_data: goods = Goods() goods.name = goods_detail['name'] goods.market_price = float( int(goods_detail['market_price'].replace('¥', '').replace('元', ''))) goods.shop_price = float( int(goods_detail['sale_price'].replace('¥', '').replace('元', ''))) goods.brief = goods_detail['desc'] if goods_detail[ 'desc'] is not None else '' goods.desc = goods_detail['goods_desc'] if goods_detail[ 'goods_desc'] is not None else '' # 取第一张作为封面图 goods.cover = goods_detail['images'][0] if goods_detail['images'] else '' category_name = goods_detail['categorys'][-1] category = Category.objects.filter(name=category_name) if category: goods.category = category[0] goods.save() for goods_image in goods_detail['images']: goods_image_instance = DetailSlide() goods_image_instance.image = goods_image goods_image_instance.goods = goods goods_image_instance.save()
import os, sys sys.path.append(os.path.dirname(os.path.abspath(__file__))) os.environ.setdefault("DJANGO_SETTINGS_MODULE", "gulishop.settings") import django django.setup() from goods.models import Goods, GoodsCategory, GoodsImage from db_tools.data.product_data import row_data for item in row_data: goods = Goods() goods.name = item['name'] goods.goods_brief = item['desc'] if item['desc'] else '' goods.desc = item['goods_desc'] if item['goods_desc'] else '' goods.market_price = float(item['market_price'].replace('¥', '').replace('元', '')) goods.shop_price = float(item['sale_price'].replace('¥', '').replace('元', '')) goods.goods_front_image = item["images"][0] if item["images"] else '' # 我们导入的数据当中存储的是类别的名字,而不是类别的对象,如果我们要去给外键赋值,得找到这个类别的对象/或者对象id(对象/id映射) category_name = item['categorys'][-1] category_list = GoodsCategory.objects.filter(name=category_name) # filter/all 拿到的是对象的列表 if category_list: goods.category = category_list[0] goods.save() for image in item['images']: goods_image = GoodsImage() goods_image.goods = goods
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 Goods,GoodsCategory,GoodsImage#必须在django初始化之后才能引入 from db_tools.data.product_data import row_data for goods_detail in row_data: goods=Goods() goods.name=goods_detail["name"] goods.market_price=float(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'] is not None else "" goods.desc=goods_detail['goods_desc'] if goods_detail['goods_desc'] is not None else "" goods.goods_front_image=goods_detail['images'][0] if goods_detail['images'] else "" category_name=goods_detail['categorys'][-1] category=GoodsCategory.objects.filter(name=category_name) if category: goods.category=category[0] goods.save() for goods_image in goods_detail['images']: goods_image_obj=GoodsImage() goods_image_obj.goods=goods goods_image_obj.image=goods_image goods_image_obj.save()
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Freshshop.settings') import django django.setup() from db_tools.data import product_data from goods.models import Goods, GoodsBannerImage, GoodsCategory for product in product_data.row_data: goods = Goods() goods.name = product['name'] goods.market_price = float( int(product['market_price'].replace('¥', '').replace('元', ''))) goods.shop_price = float( int(product['sale_price'].replace('¥', '').replace('元', ''))) goods.goods_detail = product['goods_desc'] if product[ 'goods_desc'] is not None else '' goods.desc = product['desc'] if product['desc'] is not None else '' goods.image = product['images'][0] if product['images'][ 0] is not None else '' category_name = product['categorys'][-1] category = GoodsCategory.objects.filter(name=category_name) if category: goods.category = category[0] goods.save() for image in product['images']: goods_image = GoodsBannerImage() goods_image.goods = goods goods_image.image = image goods_image.save()