def add_random_product(): category = Category.objects.first() item = Product() item.category = category item.name = my_random_string(category.slug) item.slug = item.name + '_slug' item.price = 1 item.stock = 2 item.save() return item
def init_products(): categories = Category.objects.all() for category in categories: for number in range(4): item = Product() item.category = category item.name = my_random_string(category.slug) item.slug = item.name + '_slug' item.price = 1 item.stock = 2 item.save()
project_dir = "C:/Users/Admin/Desktop/sample/enginesearch1/enginesearch/" sys.path.append(project_dir) os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' import django django.setup() from shop.models import Product data2 = csv.reader( open("C:/Users/Admin/Desktop/sample/enginesearch1/Product1.csv"), delimiter=",") for row in data2: if row[0] != 'number_id': product = Product() product.number_id = row[0] product.name = row[1] product.slug = row[2] product.description = row[3] product.price = row[4] product.available = row[5] product.stock = row[6] product.created_at = row[7] product.updated_at = row[8] product.image = row[9] product.save()
6, 'Electronic', 'Samsung S9', 'Samsung_S9', 'products/2018/04/19/S9.jpg', 'Samsung Galaxy S9', 1099, 150, 0 ], ] server = Server() for pic in pic: try: p = Product.objects.get(pid=pic[0]) except: p = Product() p.pid = pic[0] category = pic[1] try: c = Category.objects.get(name=category) except: c = Category.objects.create(name=category, slug=category) p.category = c p.name = pic[2] p.slug = pic[3] p.image = pic[4] p.description = pic[5] p.price = pic[6] p.stock = pic[7] p.whnum = pic[8] p.save() print("fin") else: continue server.close()