def goods_upload(request): r = json.loads(request.body) title = r['title'] detail = r['detail'] category = r['category'] # {url: url1, hash: hash1} # url通过 | 分割多个图片链接存在一个字段,hash同理 url_array = r['url'] seller_id = r['sellerId'] price = r['price'] express = r['express'] area = r['area'] url = '' for item in url_array: url = url + str(item['url']) + '|' img_hash = '' for item in url_array: img_hash = img_hash + str(item['hash']) + '|' goods = Goods() goods.status = 2 goods.title = title goods.detail = detail goods.category = category # 去除最后多余的| goods.url = url[:-1] goods.img_hash = img_hash[:-1] goods.seller_id = seller_id goods.price = price goods.express = express # 去除最后多余的, goods.area = area[:-1] goods.save() return JsonResponse({'success': True})
pwd = os.path.dirname(os.path.realpath(__file__)) sys.path.append(pwd + "../") os.environ.setdefault("DJANGO_SETTINGS_MODULE", "OnlineMarket.settings") import django django.setup() from apps.goods.models import Goods, GoodsCategory, GoodsImage from db_tools.data.my_product_data import row_data for goods_detail in row_data: goods = Goods() goods.name = goods_detail["name"] goods.price = float( int(goods_detail["price"].replace("¥", "").replace("元", ""))) goods.goods_intro = goods_detail["desc"] if goods_detail[ "desc"] is not None else "" goods.goods_front_img = goods_detail["images"][0] if goods_detail[ "images"] else "" goods.goods_sn = hashlib.md5( (str(int(time.time())) + goods.name).encode('utf-8')).hexdigest() 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()