migrate_shop_products working
This commit is contained in:
@@ -60,7 +60,7 @@ def migrate_shop_products(url, key, secret, version="wc/v3"):
|
|||||||
counter = 0
|
counter = 0
|
||||||
products_created = []
|
products_created = []
|
||||||
for product in products:
|
for product in products:
|
||||||
instance_data = {'company':company}
|
instance_data = {'company':company.id}
|
||||||
# parse the product info
|
# parse the product info
|
||||||
for key in product:
|
for key in product:
|
||||||
if key in product_fields:
|
if key in product_fields:
|
||||||
@@ -71,15 +71,24 @@ def migrate_shop_products(url, key, secret, version="wc/v3"):
|
|||||||
instance_data.pop('tags')
|
instance_data.pop('tags')
|
||||||
if instance_data.get('attributes') == []:
|
if instance_data.get('attributes') == []:
|
||||||
instance_data.pop('attributes')
|
instance_data.pop('attributes')
|
||||||
# create instance
|
# create instance with serializer
|
||||||
|
serializer = ProductSerializer(data=instance_data)
|
||||||
|
if serializer.is_valid():
|
||||||
|
new = serializer.save()
|
||||||
|
else:
|
||||||
|
logging.error(f"{serializer.errors}")
|
||||||
|
continue
|
||||||
|
'''
|
||||||
|
# alternative method
|
||||||
try:
|
try:
|
||||||
new = Product.objects.create(**instance_data)
|
new = Product.objects.create(**instance_data)
|
||||||
products_created.append(new)
|
|
||||||
counter += 1
|
|
||||||
logging.info(f"Product {instance_data.get('name')} created")
|
|
||||||
print(f"Product {instance_data.get('name')} created")
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"Could not create product instance: {str(e)}")
|
logging.error(f"Could not create product instance: {str(e)}")
|
||||||
|
'''
|
||||||
|
products_created.append(new)
|
||||||
|
counter += 1
|
||||||
|
logging.info(f"Product {instance_data.get('name')} created")
|
||||||
|
print(f"Product {instance_data.get('name')} created")
|
||||||
|
|
||||||
print(f"Products created: {counter}")
|
print(f"Products created: {counter}")
|
||||||
return products_created
|
return products_created
|
||||||
|
|||||||
Reference in New Issue
Block a user