def import_product_data(self, datas): from goods.models import Goods from goods.models import GoodsCategory from goods.models import GoodsImage for data in datas: good = Goods() good.name = data['name'] # 商品类别为外键 # 通过名称筛选出类别再赋值 category = data['categorys'][-1] good_category = GoodsCategory.objects.filter(name=category) if category: good.category = good_category[0] good.market_price = float(data['market_price'].replace('¥', '').replace('元', '')) good.shop_price = float(data['sale_price'].replace('¥', '').replace('元', '')) good.goods_brief = data['desc'] if data['desc'] is not None else '' good.goods_front_image = data['images'][0] if data['images'] is not None else '' good.goods_desc = data['goods_desc'] if data['goods_desc'] is not None else '' good.save() if data['images'] is not None: for image in data['images']: good_image = GoodsImage() good_image.goods = good good_image.image = image good_image.save()
def addgoods(self): for books_cate in book_data: category = GoodsCategory.objects.filter(pk=books_cate["category"]) if category: for book in books_cate["books"]: goods = Goods() goods.category = category[0] goods.name = book["name"] goods.goods_sn = book["ISBN"] goods.author = book["author"] goods.click_num = goods.sold_num = goods.fav_num = 0 goods.goods_num = 200 goods.shop_price = book["shop_price"] goods.market_price = goods.shop_price * 1.25 goods.bid = goods.shop_price * 0.8 goods.goods_brief = book["goods_brief"] goods.goods_desc = book["goods_desc"] goods.goods_front_image = book["images"][0] goods.is_hot = goods.is_new = False goods.save() book_image_list = [] for book_image in book["images"]: book_image_instance = GoodsImage() book_image_instance.image = book_image book_image_instance.goods = goods book_image_list.append(book_image_instance) GoodsImage.objects.bulk_create(book_image_list)
def deal_product(data): for goods_detail in 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.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_instance = GoodsImage() goods_image_instance.image = goods_image goods_image_instance.goods = goods goods_image_instance.save()
def import_goods_data(): from goods.models import Goods, GoodsCategory, GoodsImage with open('data/product_data.json', 'r', encoding='utf8') as f: row_data = json.loads(f.read()) 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.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_instance = GoodsImage() goods_image_instance.image = goods_image goods_image_instance.goods = goods goods_image_instance.save()
def goods_detail(request): if request.method == 'GET': types = GoodsCategory.CATEGORY_TYPE id = request.GET.get('id') if not id: return render(request, 'goods_detail.html', {'types': types}) good = Goods.objects.get(pk=id) return render(request, 'goods_detail.html', { 'good': good, 'types': types }) if request.method == 'POST': types = GoodsCategory.CATEGORY_TYPE form = GoodsForm(request.POST, request.FILES) id = request.GET.get('id') if form.is_valid(): # 验证通过 # 两种情况,一种是修改,一种是创建 good = Goods.objects.filter(pk=request.GET.get('id')).first() if not id: if form.cleaned_data.get(id) != id: return render(request, 'goods_detail.html', { 'good': good, 'goods_sn': '商品号已存在', 'types': types, }) if not good: good = Goods(**form.cleaned_data) img = form.cleaned_data.pop('goods_front_image') Goods.objects.filter(pk=request.GET.get('id')).update( **form.cleaned_data) if img: # 有图片单独更新图片 good.goods_front_image = img good.save() return HttpResponseRedirect(reverse('goods:goods_list')) else: # 修改验证失败 good = Goods.objects.filter(pk=request.GET.get('id')).first() return render(request, 'goods_detail.html', { 'good': good, 'errors': form.errors, 'types': types })
def import_goods(apps, schema_editor): from goods.models import Goods, GoodsCategory, GoodsImage for goods_detail in product_data.row_data: goods = Goods() goods.name = goods_detail['name'] goods.market_price = float(goods_detail['market_price'].replace('¥', '').replace('元', '')) goods.shop_price = float(goods_detail['sale_price'].replace('¥', '').replace('元', '')) goods.goods_brief = goods_detail['desc'] if goods_detail['desc'] is not None else '' goods.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_instance = GoodsImage() goods_image_instance.image = goods_image goods_image_instance.goods = goods goods_image_instance.save()
pwd = os.path.dirname(os.path.abspath(__file__)) sys.path.append(pwd+"../") os.environ.setdefault("DJANGO_SETTINGS_MODULE", "FoodMarket.settings") import django django.setup() from goods.models import Goods, GoodsCategory, GoodsImage from db_tools.data.product_data import row_data for import_good_data in row_data: db_good = Goods() db_good.name = import_good_data["name"] # 首页图片路径保存为字符串即可 db_good.goods_front_image = import_good_data["images"][0] if import_good_data["images"] else '' db_good.market_price = float(int(import_good_data["market_price"].replace("¥", "").replace("元", ""))) db_good.shop_price = float(int(import_good_data["sale_price"].replace("¥", "").replace("元", ""))) db_good.goods_brief = import_good_data["desc"] if import_good_data["desc"] is not None else "" db_good.goods_desc = import_good_data["goods_desc"] if import_good_data["goods_desc"] is not None else "" # 取出最后一个根茎类字符串就可以了 category_name = import_good_data["categorys"][-1] # 为什么使用filter不使用get因为使用django的get方法是从数据库的取得一个匹配的结果,返回一个对象,如果记录不存在的话,它会报错, # 而且记录多于两条也会报错,而使用filter会返回结果的一个数组,当没有查询到结果时返回空数组。 goods_category = GoodsCategory.objects.filter(name=category_name) if goods_category: db_good.category = goods_category[0] db_good.save() # 把每个图片保存为对象
from goods.models import Goods, GoodsCategory, GoodsImage from db_tools.data.product_data import row_data for product in row_data: goods = Goods() goods.name = product.get('name') goods.market_price = float( int(product.get('market_price').replace("¥", "").replace("元", ""))) goods.shop_price = float( int(product.get('sale_price').replace("¥", "").replace("元", ""))) goods.goods_brief = product.get('desc') if product.get( 'desc') is not None else "" goods.goods_desc = product.get('goods_desc') if product.get( 'goods_desc') is not None else "" goods.goods_front_image = product.get('images')[0] if product.get( 'images') else "" goodsImages = product.get('images') category_name = product.get('categorys')[-1] # objects.filter会获取一个集合 category = GoodsCategory.objects.filter(name=category_name) if category: goods.category = category[0] print(goods) goods.save() print('-------images------') for imgUrl in product.get('images'): image = GoodsImage() image.goods = goods image.image = imgUrl print(image.image)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "MxShop.settings") django.setup() from goods.models import Goods, GoodsCategory, GoodsImage for goods_detail in row_data: goods_install = Goods() goods_install.name = goods_detail['name'] goods_install.market_price = float(int(goods_detail['market_price'].replace("¥", "").replace("元", ""))) goods_install.shop_price = float(int(goods_detail['sale_price'].replace("¥", "").replace("元", ""))) goods_install.goods_brief = goods_detail['desc'] and goods_detail['desc'] or '' goods_install.goods_desc = goods_detail['goods_desc'] and goods_detail['goods_desc'] or '' goods_install.goods_front_image = goods_detail['images'][0] if goods_detail['images'] else "" categorys_name = goods_detail['categorys'][-1] category = GoodsCategory.objects.filter(name=categorys_name) if category: goods_install.category = category[0] goods_install.save() for goods_image in goods_detail['images']: goods_image_instace = GoodsImage() goods_image_instace.image = goods_image goods_image_instace.goods = goods_install goods_image_instace.save()
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "gulishop.settings") import django django.setup() from db_tools.data.product_data import row_data from goods.models import Goods, GoodsCategory, GoodsImage for item in row_data: goods = Goods() goods.name = item['name'] goods.goods_brief = item['desc'] if item['desc'] else '' goods.goods_desc = item['goods_desc'] if item['goods_desc'] else '' goods.market_price = item['market_price'].replace('¥', '').replace('元', '') goods.shop_price = item['sale_price'].replace('¥', '').replace('元', '') goods.goods_front_image = item['images'][0] if item['images'] else '' category_name = item['categorys'][-1] category_list = GoodsCategory.objects.filter(name=category_name) if category_list: goods.category = category_list[0] goods.save() for image in item['images']: goods_image = GoodsImage() goods_image.goods = goods goods_image.image = image goods_image.save()
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()
from goods.models import Goods, GoodsCategory, GoodsImage from db_tools.data.product_data import row_data for good in row_data: good_detail = Goods() good_detail.name = good['name'] good_detail.market_price = float(good['market_price'].replace('¥', '').replace( '元', '')) good_detail.sale_price = float(good['sale_price'].replace('¥', '').replace( '元', '')) good_detail.goods_brief = good['desc'] if good['desc'] is not None else '' good_detail.goods_desc = good['goods_desc'] if good[ 'goods_desc'] is not None else '' good_detail.goods_front_image = good['images'][0] if good['images'] else '' c_name = good['categorys'][-1] category = GoodsCategory.objects.filter(name=c_name) if category: good_detail.category = category[0] good_detail.save() for goods_image in good['images']: goods_image_instance = GoodsImage() goods_image_instance.image = goods_image goods_image_instance.goods = good_detail goods_image_instance.save()
import django django.setup() from goods.models import Goods, GoodsCategory, GoodsImage 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"] or "" goods.goods_desc = goods_detail["goods_desc"] or "" goods.goods_front_image = goods_detail["images"][0] or "" 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_instance = GoodsImage() goods_image_instance.image = goods_image goods_image_instance.goods = goods goods_image_instance.save()
django.setup() from goods.models import Goods, GoodsCategory, GoodsImage 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 not goods_detail['desc'] is None else '' goods.goods_desc = goods_detail[ 'goods_desc'] if not goods_detail['goods_desc'] is None else '' goods.goods_front_image = goods_detail['images'][ 0] if not goods_detail['images'][0] is None 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_intance = GoodsImage() goods_image_intance.image = goods_image goods_image_intance.goods = goods goods_image_intance.save()
from goods.models import Goods, GoodsCategory, GoodsImage 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.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'] is not None 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_instance = GoodsImage() goods_image_instance.image = goods_image goods_image_instance.goods = goods goods_image_instance.save()
django.setup() from goods.models import Goods, GoodsCategory, GoodsImage from db_tools.data.product_data import row_data for good in row_data: good_instance = Goods() good_instance.name = good['name'] good_instance.goods_desc = good['goods_desc'] if good[ 'goods_desc'] is not None else '' good_instance.market_price = good['market_price'].replace('¥', '').replace( '元', '') good_instance.shop_price = good['sale_price'].replace('¥', '').replace('元', '') good_instance.goods_brief = good['desc'] if good['desc'] is not None else '' good_instance.goods_front_image = good['images'][0] if good[ 'images'] else '' category_name = good['categorys'][-1] category = GoodsCategory.objects.filter(name=category_name) if category: good_instance.category = category[0] good_instance.save() for image in good['images']: good_image = GoodsImage() good_image.image = image good_image.goods = good_instance good_image.save()
import os import sys BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.append(BASE_DIR) os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_project.settings") import django django.setup() from db_tool.data.goods_data import goods_data from goods.models import Category, Goods for data in goods_data: goods = Goods() goods.goods_id = data['goods_id'] goods.category = Category.objects.filter(pk=data['category']).first() goods.name = data['name'] goods.market_price = data['market_price'] goods.goods_brief = data['goods_brief'] goods.goods_desc = data['goods_desc'] goods.goods_front_image = data['goods_front_image'] goods.periods = data['periods'] goods.save() print('goods数据导入完成')
from db_tools.data.product_data import row_data # i = 1 for goods_detail in row_data: # print(goods_detail) goods = Goods() goods.name = goods_detail["name"] goods.market_price = float(goods_detail["market_price"].replace("¥","").replace("元","")) goods.shop_price = float(goods_detail["sale_price"].replace("¥","").replace("元","")) # 商品简单描述 # 下面语法的含义是:如果goods_detail["desc"]不为None直接返回,否则返回”“ goods.goods_brief = goods_detail["desc"] if goods_detail["desc"] is not None else "" # 商品富文本描述 goods.goods_desc = goods_detail["goods_desc"] if goods_detail["goods_desc"] is not None else "" # 商品封面取images的第一张图片 # 如果没有就返回”“ goods.goods_front_image = goods_detail["images"][0] if goods_detail["images"] is not None else "" categorys_name =goods_detail["categorys"][-1] categorys = GoodsCategory.objects.filter(name=categorys_name) if categorys: goods.category = categorys[0] goods.save() for goods_image in goods_detail["images"]: goods_image_instace = GoodsImage() goods_image_instace.image = goods_image goods_image_instace.goods = goods goods_image_instace.save()
from goods.models import Goods, GoodsCategory, GoodsImage for item in row_data: goods = Goods() goods.market_price = float(item["market_price"].replace("元", "").replace( "¥", "")) goods.shop_price = float(item["market_price"].replace("元", "").replace("¥", "")) goods.name = item["name"] if item["name"] is not None else "" # 商品的简单描述 goods.goods_brief = item["desc"] if item["desc"] is not None else "" # 商品的详细描述 goods.goods_desc = item["goods_desc"] if item["goods_desc"] else "" goods.goods_front_image = item["images"][0] if item["images"] else "" # 类目:三级类目或者二级类目 category_name = item["categorys"][-1] # 查看类目是否存在 categorys = GoodsCategory.objects.filter(name=category_name) # print("categorys===",categorys) if categorys: goods.category = categorys[0] goods.save() for image in item["images"]: goods_image = GoodsImage() goods_image.image = image
django.setup() from goods.models import Goods, GoodsCategory, GoodsImage 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.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_instance = GoodsImage() goods_image_instance.image = goods_image goods_image_instance.goods = goods goods_image_instance.save()
django.setup() from db_tools.data.product_data import row_data from goods.models import Goods, GoodsCategory, GoodsImage for goods_detils in row_data: goods = Goods() goods.name = goods_detils['name'] goods.goods_brief = goods_detils['desc'] if goods_detils[ 'desc'] is not None else "" goods.goods_desc = goods_detils['goods_desc'] if goods_detils[ 'goods_desc'] is not None else "" goods.market_price = float( int(goods_detils['market_price'].replace("¥", "").replace("元", ""))) goods.shop_price = float( int(goods_detils["sale_price"].replace("¥", "").replace("元", ""))) goods.goods_front_image = goods_detils['images'][0] if goods_detils[ 'images'] else "" category_name = goods_detils['categorys'][-1] category = GoodsCategory.objects.filter(name=category_name) if category: goods.category = category[0] goods.save() for image_detail in goods_detils['images']: image = GoodsImage() image.image = image_detail image.goods = goods image.save()
django.setup() from goods.models import Goods, GoodsCategory, GoodsImage 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.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_instance = GoodsImage() goods_image_instance.image = goods_image goods_image_instance.goods = goods goods_image_instance.save()
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "daydayfresh.settings") import django django.setup() from goods.models import Goods, GoodsCategory, GoodsImage 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.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_instance = GoodsImage() goods_image_instance.image = goods_image goods_image_instance.goods = goods goods_image_instance.save()