def make_product_group(similar_products): product_group = {} product_group['sku'] = similar_products[0].sku product_group['short_description'] = similar_products[0].short_description product_group['description'] = similar_products[0].description product_group['price'] = similar_products[0].price attributes = [] attribute_titles = get_attribute_titles(similar_products) for i,product in enumerate(similar_products,start=1): attribute = {} attribute['name'] = attribute_titles[i] attribute['sku'] = product.sku attribute['short_description'] = product.short_description attribute['description'] = product.description attribute['price'] = product.price attributes.append(attribute) product_group['name'] = similar_products[0].name # save to database for product in similar_products: product.delete() head_product = similar_products[0] head_product.save() # save attributes for attribute_map in attributes: attribute = Attribute(attribute_map) attribute.save() return product_group
if attribute.price == 0: try: p = Product.objects.filter(sku=datas[1].string[1:]).get() attribute.price = p.price except: # has_valid_prices = False # print "attribute had no previous price, sku = " + datas[1].string[1:] products_not_added.append(datas[1].string[1:]) null_price = True attribute.sku = datas[1].string[1:] attribute.description = datas[5].string attribute.short_description = datas[11].string # attributes.append(attribute) if not null_price: attribute.save() else: products_not_added.append(datas[1].string[1:]) # if(has_valid_prices): # product.save() # for attribute in attributes: # attribute.save() # else: # products_not_added_skus.append(product.sku) # for attribute in attributes: # products_not_added_skus.append(attribute.sku) not_added_products_file = open("not_added_products_files.json","w") not_added_products_file.write(json.dumps(products_not_added,indent=4))