diff --git a/utils/woocommerce.py b/utils/woocommerce.py index 01c0a3f..40ab6b6 100644 --- a/utils/woocommerce.py +++ b/utils/woocommerce.py @@ -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) - 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)}") + ''' + 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}") return products_created