importing woocommerce products is working

This commit is contained in:
Sam
2021-02-17 12:26:40 +00:00
parent 4140250174
commit 42097c3102
2 changed files with 48 additions and 19 deletions

View File

@@ -67,24 +67,40 @@ def migrate_shop_products(url, key, secret, version="wc/v3"):
instance_data[key] = product[key]
# remove unwanted fields
instance_data.pop('id')
if instance_data.get('tags') == []:
instance_data.pop('tags')
if instance_data.get('attributes') == []:
instance_data.pop('attributes')
# extract m2m field data
tags = instance_data.pop('tags')
attributes = instance_data.pop('attributes')
# create instance with serializer
'''
serializer = ProductSerializer(data=instance_data)
if serializer.is_valid():
new = serializer.save()
if tags:
new.tags.set(tags)
if attributes:
new.attributes.set(attributes)
new.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)}")
'''
serializer = ProductSerializer(data=instance_data)
if serializer.is_valid():
try:
new = Product.objects.create(**serializer.validated_data)
if tags:
new.tags.set(tags)
if attributes:
new.attributes.set(attributes)
new.save()
except Exception as e:
logging.error(f"Could not create product instance: {str(e)}")
else:
logging.error(f"{serializer.errors}")
continue
products_created.append(new)
counter += 1
logging.info(f"Product {instance_data.get('name')} created")