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
|
||||
products_created = []
|
||||
for product in products:
|
||||
instance_data = {'company':company}
|
||||
instance_data = {'company':company.id}
|
||||
# parse the product info
|
||||
for key in product:
|
||||
if key in product_fields:
|
||||
@@ -71,15 +71,24 @@ def migrate_shop_products(url, key, secret, version="wc/v3"):
|
||||
instance_data.pop('tags')
|
||||
if instance_data.get('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:
|
||||
new = Product.objects.create(**instance_data)
|
||||
except Exception as 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")
|
||||
except Exception as e:
|
||||
logging.error(f"Could not create product instance: {str(e)}")
|
||||
|
||||
print(f"Products created: {counter}")
|
||||
return products_created
|
||||
|
||||
Reference in New Issue
Block a user