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_upload(request,store_id): goods_1_type = GoodsType.objects.filter(parent__isnull=True) store = Store.objects.get(pk=store_id) if request.method == 'GET': return render(request,'goods/goods_upload.html',{'store':store,'goods_1_type':goods_1_type}) elif request.method == 'POST': name = request.POST['name'] price = request.POST['price'] stock = request.POST['stock'] intro = request.POST['intro'].strip() imgpath = request.FILES.get('imgpath') if name == "" or price == "" or stock == "" or intro == "" or imgpath == None: return render(request,'goods/goods_upload.html',{'store':store,'goods_1_type':goods_1_type,'error_msg':'字段不能为空!'}) # 其他数据 type_id = request.POST['type2'] goodstype = GoodsType.objects.get(pk=type_id) _store = Store.objects.get(pk=store_id) # 创建商品对象,完成上传操作 try: goods = Goods(name=name,price=price,stock=stock,intro=intro,goodstype=goodstype,store=_store) goods.save() except: return render(request, 'goods/goods_upload.html', {'store': store, 'goods_1_type': goods_1_type, 'error_msg': '上传字段有误'}) # 创建商品图片对象,保存商品图片 goods_image = GoodsImage(path=imgpath,goods=goods) goods_image.save() # 使用redirect 传递参数 return redirect(reverse('goods:goods_detail',args={store_id}))
def post(self, request, format=None): file_obj = request.data["file"] data = pd.read_excel(file_obj) try: for index, row in data.iterrows(): goods = Goods() goods.gtin = row['id'] goods.company = row['company'] goods.spec = row['spec'] goods.name = row['name'] goods.brand = row['brand'] if len(eval(row['classes'])) > 0: goods.is_class = 1 goods.save() classes = eval(row['classes']) for class_name in classes: category = GoodsCategory.objects.filter( name=class_name) goods.classes.add(category[0]) else: goods.save() except Argument: print(Argument) return Response({"msg": "上传文件失败,请检查文件格式是否正确"}) else: return Response(status=201, data={"code": 201, "msg": "上传数据成功"}) # print(data) # print("here") # return Response(status=201) # def DashboardView(self): # total = Goods.objects.all().count() # is_class = Goods.objects.filter(is_class=1).count() # no_class = total - is_class # class_per = is_class / total # json_data = { # "total": total, # "is_class": is_class, # "no_class": no_class, # "percent": class_per # } # return JsonResponse(json_data,safe=False) # class DashboardView(View): # def get(self, request): # total = Goods.objects.all().count() # is_class = Goods.objects.filter(is_class=1).count() # no_class = total - is_class # class_per = is_class / total # json_data = { # "total": total, # "is_class": is_class, # "no_class": no_class, # "percent": class_per # } # return JsonResponse(json.dump(json_data),safe=False)
def addGoods(request): goods=Goods() goods.name=request.GET['name'] goods.description=request.GET['description'] goods.picture1=open("tmp\\nokia1.jpg","rb") goods.picture2=open("tmp\\nokia2.jpg","rb") goods.picture3=open("tmp\\nokia3.jpg","rb") goods.price=105 # goods.time=datetime.datetime.now goods.save() return HttpResponse("success")
def goodsCreate(request): print(request.POST) name = request.POST.get('name',None) goodsQuery = Goods.objects.filter(name=name) if goodsQuery : res = {"success":False,"err":"商品名字重复"} else: res = {"success": True, "err": None} costprice = float(request.POST.get('costprice',0)) saleprice = float(request.POST.get('saleprice', 0)) initotal = int(request.POST.get('initotal', 0)) desc = request.POST.get('desc','') goods = Goods(name=name,costprice=costprice,saleprice=saleprice,initotal=initotal,desc=desc) goods.save() return HttpResponse(json.dumps(res),content_type="application/json")
def __init__(self): super(GoodsCreater, self).__init__(0) counter = 300 while counter != 0: new_object = Goods() new_object.supplier = self.rdm_choice(self.LIST_OF_SUPPLIERS) new_object.name = self.rdm_choice(self.LIST_OF_GOODS_NAMES) new_object.price = randrange(0, 100) path = self.user_goods_directory_path( new_object, self.rdm_choice(self.LIST_OF_PICTURES)) print(path) new_object.picture = path new_object.description = self.rdm_choice(self.LIST_OF_DESCRIPTION) new_object.save() print(new_object) counter -= 1
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()
#商品详细介绍 instance.goods_desc = goods_detail["goods_desc"] if goods_detail[ "goods_desc"] else "" #商品的封面 instance.goods_front_image = goods_detail["images"][0] if goods_detail[ "images"] else "" #设置商品属于哪个商品类别 categorys_name = goods_detail["categorys"][-1] #'根茎类',或者 #查询数据库看看根茎类,是否存在 categorys = GoodsCategory.objects.filter(name=categorys_name) #返回的是列 if categorys: #商品数据哪个类目 instance.category = categorys[0] #保存商品数据 instance.save() #保存商品轮播图 for image in goods_detail["images"]: print(image) goods_image = GoodsImage() #商品--商品轮播图和商品建立关系 goods_image.goods = instance #图片 goods_image.image = image #保存商品轮播图 goods_image.save()
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()
#商户的描述对应goods_detail中的goods_desc内容,如果goods_detail中的goods_desc内容不为空,就设置为空串 goods.goods_desc = goods_detail["goods_desc"] if goods_detail["goods_desc"] is not None else "" #商品的封面图等于goods_detail中images中的第一张图,如果没有这么多图片,则设置为空串 goods.goods_front_image = goods_detail["images"][0] if goods_detail["images"] else "" #商品类别的名字等于goods_detail中categorys中的倒数第一个 category_name = goods_detail['categorys'][-1] #有了商品的名字 #此处用filter(),不用get(),是因为filter()获取不到数据的时候,返回的是一个空的数组,不会抛异常, #如果用get(),情况一:数据库里没有;情况二:数据库里查到两条;这两种情况下都是会抛异常的, #用get()的话,外面必须加一个try catch,用filter()就不用加try catch category = GoodsCategory.objects.filter(name=category_name) if category: #如果查到 goods.category = category[0] #商品的类别选搜索到的category的第一个 goods.save() #保存 #图片有轮播图,现在将图片保存起来 for goods_image in goods_detail["images"]: goods_image_instance = GoodsImage() #实例化 goods_image_instance.image = goods_image #实例化对象中的image字段(图片路径)等于goods_image goods_image_instance.goods = goods #实例化对象中的goods字段等于上述的goods goods_image_instance.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()
def parser(): proxies = { 'https': 'https://51.140.202.119:8080' } category_list =['https://www.avito.ru/moskva/tovary_dlya_kompyutera/komplektuyuschie/videokarty'] # 'https://www.avito.ru/moskva/tovary_dlya_kompyutera/komplektuyuschie/cd_dvd_i_blu-ray_privody', # 'https://www.avito.ru/moskva/tovary_dlya_kompyutera/komplektuyuschie/bloki_pitaniya', # 'https://www.avito.ru/moskva/tovary_dlya_kompyutera/komplektuyuschie/videokarty', # 'https://www.avito.ru/moskva/tovary_dlya_kompyutera/komplektuyuschie/zhestkie_diski', # 'https://www.avito.ru/moskva/tovary_dlya_kompyutera/komplektuyuschie/zvukovye_karty', # 'https://www.avito.ru/moskva/tovary_dlya_kompyutera/komplektuyuschie/kontrollery', # 'https://www.avito.ru/moskva/tovary_dlya_kompyutera/komplektuyuschie/korpusy', # 'https://www.avito.ru/moskva/tovary_dlya_kompyutera/komplektuyuschie/materinskie_platy', # 'https://www.avito.ru/moskva/tovary_dlya_kompyutera/komplektuyuschie/operativnaya_pamyat', # 'https://www.avito.ru/moskva/tovary_dlya_kompyutera/komplektuyuschie/protsessory', # 'https://www.avito.ru/moskva/tovary_dlya_kompyutera/komplektuyuschie/sistemy_ohlazhdeniya'] category_dict = {'https://www.avito.ru/moskva/tovary_dlya_kompyutera/komplektuyuschie/videokarty': 'videokarty'} # 'https://www.avito.ru/moskva/tovary_dlya_kompyutera/komplektuyuschie/cd_dvd_i_blu-ray_privody': # 'cd_dvd_i_blu-ray_privody', 'https://www.avito.ru/moskva/tovary_dlya_kompyutera/komplektuyuschie/bloki_pitaniya': # 'bloki_pitaniya', 'https://www.avito.ru/moskva/tovary_dlya_kompyutera/komplektuyuschie/videokarty': # 'videokarty', 'https://www.avito.ru/moskva/tovary_dlya_kompyutera/komplektuyuschie/zhestkie_diski': # 'zhestkie_diski', 'https://www.avito.ru/moskva/tovary_dlya_kompyutera/komplektuyuschie/zvukovye_karty': # 'zvukovye_karty', 'https://www.avito.ru/moskva/tovary_dlya_kompyutera/komplektuyuschie/kontrollery': # 'kontrollery', 'https://www.avito.ru/moskva/tovary_dlya_kompyutera/komplektuyuschie/korpusy': 'korpusy', # 'https://www.avito.ru/moskva/tovary_dlya_kompyutera/komplektuyuschie/materinskie_platy': 'materinskie_platy', # 'https://www.avito.ru/moskva/tovary_dlya_kompyutera/komplektuyuschie/operativnaya_pamyat': 'operativnaya_pamyat', # 'https://www.avito.ru/moskva/tovary_dlya_kompyutera/komplektuyuschie/protsessory': 'protsessory', # 'https://www.avito.ru/moskva/tovary_dlya_kompyutera/komplektuyuschie/sistemy_ohlazhdeniya': 'sistemy_ohlazhdeniya'} for category in category_list: avito_html = requests.get(category, proxies=proxies) text = BeautifulSoup(avito_html.text, "html.parser") pages = text.select('.pagination-page') print(text) # Определяет кол-во страниц, на которых расположены нужные объявления for page in pages: if page.getText() == 'Последняя': m = re.search('(p=)\w+', page.get('href')) total_pages = int(m.group(0)[2:]) # Ходит по страницам, ищет ссылки на товар и записывает их в список links links = set() y = 0 for number_page in range(1, total_pages + 1): link = category + '?p=' + str(number_page) #Обработка исключений без прерывания цикла try: avito_html = requests.get(link, proxies=proxies) except Exception as e: print(e) continue text = BeautifulSoup(avito_html.text, "html.parser") items = text.select('.item-description-title-link') for i in items: href = 'https://www.avito.ru' + i.get('href') links.add(href) print(href) # y += 1 # if y == 1: # break time.sleep(1) print(links) # Ходит по ссылкам на товар и заполняет список словарей months = {'марта': '03', 'апреля': '04', 'мая': '05', 'июня': '06', 'июля': '07', 'августа': '08', 'сентября': '09', 'октября': '10', 'ноября': '11', 'декабря': '12', 'января': '01', 'февраля': '02'} x = 0 for item_link in links: try: item_html = requests.get(item_link, proxies=proxies) item_text = BeautifulSoup(item_html.text, "html.parser") info_dict = {} meta_data_list = item_text.select('.title-info-metadata-item')[0].getText().split() if meta_data_list[3] == "сегодня": info_dict['avito_date_publication'] = datetime.now().strftime('%Y-%m-%d') elif meta_data_list[3] == 'вчера': yesterday = datetime.now() - timedelta(days=1) info_dict['avito_date_publication'] = yesterday.strftime('%Y-%m-%d') else: info_dict['avito_date_publication'] = datetime.now().strftime('%Y') + '-' + months[ meta_data_list[4]] + '-' + meta_data_list[3] info_dict['avito_time_publication'] = meta_data_list[-1] info_dict['id'] = meta_data_list[1].strip(',') info_dict['name'] = item_text.select('.title-info-title-text')[0].getText() info_dict['price'] = item_text.select('.js-item-price')[0].getText() info_dict['adress'] = ''.join(item_text.select('.item-map-location')[0].getText().split()[1:-2]) info_dict['ad_text'] = item_text.select('.item-description-text')[0].getText() # info_dict['phone'] = item_text.select('.item-popup-content') image_tags = item_text.select('.gallery-img-frame') image_links = '' for image_tag in image_tags: image_links += image_tag['data-url'] + ',' info_dict['photo_link'] = image_links print(info_dict) #Записываем данные из словарей в бд, через модель django good = Goods(name=info_dict['name'], avito_ad_number=info_dict['id'], publication_date=info_dict['avito_date_publication'], publication_time=info_dict['avito_time_publication'], text=info_dict['ad_text'], photo_link=info_dict['photo_link'], adress=info_dict['adress'], price=info_dict['price'], category=category_dict[category]) good.save() # x += 1 # if x == 2: # break time.sleep(1) except BaseException as er: print(er) continue
def upload_goods(request): if not request.session.get('userName', None): return render(request, "login.html") if request.method == "POST": name = request.POST.get('goodsname') detail_msg = request.POST.get('desc') type = request.POST.get('type') type = dicts[type] price = request.POST.get('price') state = 0 num = request.POST.get('num') image = request.FILES.get('image') nick_name = request.session.get('userName') owner = UserProfile.objects.get(nick_name=nick_name) print(name, detail_msg, type, price, num, image, owner) if not all([owner, num, price, type, detail_msg, name]): return render(request, 'upload_goods.html', {'errmsg': '请将信息补充完整!'}) try: if type == 1: from_time = request.POST.get('from_time') to_time = request.POST.get('to_time') print(from_time, to_time) new_goods = Goods(name=name, desc=detail_msg, type=type, fromtime=from_time, totime=to_time, price=price, state=state, num=num, owner=owner) elif type == 2: location = request.POST.get('location') new_goods = Goods(name=name, desc=detail_msg, type=type, location=location, price=price, state=state, num=num, owner=owner) else: new_goods = Goods(name=name, desc=detail_msg, type=type, price=price, state=state, num=num, owner=owner) if image != None: new_goods.image = image new_goods.save() except Exception as e: print("e: ", e) # 把异常打印出来 return render(request, 'upload_goods.html', { 'username': nick_name, 'errmsg': '未知类型错误' }) # 注册完,还是返回注册页。 return render(request, 'upload_goods.html', {'username': nick_name}) nick_name = request.session.get('userName') return render(request, 'upload_goods.html', {'username': nick_name})
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 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()
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()
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() # 把每个图片保存为对象 if import_good_data["images"]: for image in import_good_data["images"]: goods_image = GoodsImage() goods_image.image = image goods_image.goods = db_good goods_image.save()
''' if __name__ == "__main__": from goods.models import Goods, GoodsCategory, GoodsImage from db_tools.data.product_data import row_data for g in row_data: g_instance = Goods() g_instance.name = g['name'] g_instance.market_price = float( int(g["sale_price"].replace("¥", "").replace("元", ""))) g_instance.shop_price = float( int(g["sale_price"].replace("¥", "").replace("元", ""))) g_instance.goods_brief = g['desc'] if g['desc'] is not None else '' g_instance.goods_desc = g['goods_desc'] if g[ 'goods_desc'] is not None else '' g_instance.goods_front_image = g['images'][0] if g['images'] else '' # 类别 category = GoodsCategory.objects.filter(name=g['categorys'][-1]) if category: g_instance.category = category[0] g_instance.save() # goodsimage for img in g['images']: good_img_instane = GoodsImage() good_img_instane.goods = g_instance good_img_instane.image = img good_img_instane.save()