我为某些代码而生气。执行随机生成产品建议的程序总是会导致错误
TypeError:无法解包不可迭代的NoneType对象
但我似乎看不到返回NoneType对象的位置。所有打印语句均清楚表明我返回了一个对象和一个布尔值。不幸的是,这是很多代码,而且我似乎无法用更少的代码来重现错误。
@staticmethod
def generate_random_product(variation):
product_options = Product.objects.all()
if not product_options:
raise ValidationError(
'PRODUCT CREATION FAILED HARD COULD NOT '
'GENERATE RANDOM PRODUCT FOR: '
+ variation.name + '... PLEASE DELETE'
'ALL DELIVERIES THAT WERE CREATED TODAY'
)
random_int = random.randint(0, (product_options.count() - 1))
return product_options[random_int], True
def generate_random_product_from_category(self, variation, count):
if count >= 10:
return self.generate_random_product(variation, count)
category = variation.package.category
product_options = category.product_set.all()
if not product_options:
return self.generate_random_product(variation)
random_int = random.randint(0, (product_options.count() - 1))
return product_options[random_int], True
def generate_random_product_from_variation(self, variation, count):
if count >= 5:
return self.generate_random_product_from_category(variation, count)
product_options = variation.product_set.all()
if not product_options:
return self.generate_random_product_from_category(variation)
random_int = random.randint(0, (product_options.count() - 1))
return product_options[random_int], False
def turn_variation_into_product(self, variation, delivery, count):
if count < 15:
# STEP 1: generate a random product
random_product, failed_auto = self.generate_random_product_from_variation(variation, count)
print(random_product)
try:
self.turn_variation_into_product(variation, delivery, count + 1)
except (DeliveryItem.DoesNotExist) as e:
print('return')
print(random_product, failed_auto)
return random_product, failed_auto
else:
delivery.delete()
raise ValidationError(
f'all options for {variation.name} are already '
f'included in delivery {delivery.slug} ... tried '
f'*{count}* times')
def create_delivery_items(self, delivery):
schema_items = delivery.delivery_schema.schemaitem_set.all()
for schema_item in schema_items:
if schema_item.variation and not schema_item.product:
random_product, failed_auto = self.turn_variation_into_product(schema_item.variation, delivery, 0)
DeliveryItem.objects.create(
quantity=schema_item.quantity,
variation=schema_item.variation,
product=random_product,
original_type=schema_item.type,
delivery=delivery,
failed_auto=failed_auto,
)
此行导致错误:
random_product,failed_auto = self.turn_variation_into_product(schema_item.variation,delivery,0)
如您所见,我在相关return语句的正上方有print语句。但是print语句会输出正确的对象。