gpt4 book ai didi

python - Django 模型返回 NoneType

转载 作者:行者123 更新时间:2023-11-28 20:30:00 26 4
gpt4 key购买 nike

我有一个模型产品

它有两个字段大小和颜色等等

colours = models.CharField(blank=True, null=True, max_length=500)
size = models.CharField(blank=True, null=True, max_length=500)

在我看来我有

current_product = Product.objects.get(slug=title)
if len(current_product.size) != 0 :
current_product.size = current_product.size.split(",")

并得到这个错误:

'NoneType' 类型的对象没有 len()

什么是 NoneType 以及如何测试它?

最佳答案

NoneTypeNone 值的类型。你想将第二个片段更改为

if current_product.size: # This will evaluate as false if size is None or len(size) == 0.
blah blah

关于python - Django 模型返回 NoneType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/552521/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com