from ..models import Product def save_product(product): #check if product is already in the database and store_name is same if Product.objects.filter(product_id=product['product_id'], store_name=product['store_name']).exists(): # update the product product_to_update = Product.objects.filter(product_id=product['product_id'], store_name=product['store_name'])[0] product_to_update.title = product['title'] product_to_update.ingredients = product['ingredients'] product_to_update.url = product['url'] product_to_update.save() return False new_product = Product(title=product['title'], ingredients=product['ingredients'], product_id=product['product_id'], url=product['url'], store_name=product['store_name']) new_product.save() return True