【问题标题】:TypeError: object of type 'bool' has no len() i cant find problemTypeError: 'bool' 类型的对象没有 len() 我找不到问题
【发布时间】:2020-06-24 08:21:27
【问题描述】:

我得到 TypeError: 'bool' 类型的对象在 django admin 中没有 len

型号

models.py

# Create your models here.
class Products(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    name = models.CharField(max_length=50, blank=False, null=False)
    slug = models.CharField(max_length=50, blank=True, null=True)
    default_image = models.ImageField(upload_to='products', blank=False)
    stock = models.IntegerField(blank=False, null=False)
    brand = models.ForeignKey(ProductBrand, on_delete=models.CASCADE)
    price = models.CharField(max_length=15, blank=True, null=True)
    on_sale = models.BooleanField(default=False)
    createAt = date.jDateField(auto_now_add=True, editable=False)
    description = models.TextField(max_length=250, blank=True, null=True)
    small_description = models.TextField(max_length=150, blank=True, null=True, )
    variableProduct = models.BooleanField(default=False)
    category = models.ManyToManyField(Category, default=False, blank=True)
    productTags = models.ManyToManyField(ProductTags, default=False, blank=True)

    @property
    def username(self):
        return self.user.username

admin.py

class ProductsModelView(admin.ModelAdmin):
    formfield_overrides = {
        models.TextField: {'widget': Textarea(attrs={'dir': 'rtl', })},
    }
    model = Products
    list_display = ('name', 'id', 'username')
admin.site.register(Products, ProductsModelView)

错误

TypeError at /admin/products/products/add/
object of type 'bool' has no len()
Request Method: POST
Request URL:    http://192.168.1.199:8000/admin/products/products/add/
Django Version: 3.0.7
Exception Type: TypeError
Exception Value:    
object of type 'bool' has no len()
Exception Location: E:\GOA\GOA\venv1\lib\site-packages\django\forms\models.py in has_changed, line 1354
Python Executable:  E:\GOA\GOA\venv1\Scripts\python.exe
Python Version: 3.7.7
Python Path:    
['E:\\GOA\\GOA',
 'C:\\Users\\pc\\AppData\\Local\\Programs\\Python\\Python37\\python37.zip',
 'C:\\Users\\pc\\AppData\\Local\\Programs\\Python\\Python37\\DLLs',
 'C:\\Users\\pc\\AppData\\Local\\Programs\\Python\\Python37\\lib',
 'C:\\Users\\pc\\AppData\\Local\\Programs\\Python\\Python37',
 'E:\\GOA\\GOA\\venv1',
 'E:\\GOA\\GOA\\venv1\\lib\\site-packages',
 'E:\\GOA\\GOA\\venv1\\lib\\site-packages\\setuptools-40.8.0-py3.7.egg']
Server time:    Wed, 24 Jun 2020 08:11:15 +0000

我尝试在 django admin 中保存对象,但出现此错误,但使用 python shell 创建对象没有问题

【问题讨论】:

  • ManyToManyField(Category, default=False 看起来不对。尝试删除 ManyToManyFields 的默认值。
  • 你在某处有一个布尔变量,它被传递给 len() 函数。从您发布的代码很难看出。
  • @AlexHall 谢谢我检查了很多次但我不明白
  • 数据错误确实不合逻辑

标签: python django


【解决方案1】:
  • auto_now - 每次更新字段的值到当前时间和日期 调用 Model.save()。

  • auto_now_add - 使用创建记录的时间和日期更新值。

  • 任何设置了 auto_now 属性的字段也将继承 editable=False

改变这个

createAt = date.jDateField(auto_now_add=True, editable=False)

到这里

createAt = models.DateTimeField(auto_now_add =True)

OR

createAt = models.DateTimeField(auto_now =True)

【讨论】:

    猜你喜欢
    • 2019-02-08
    • 1970-01-01
    • 2015-08-21
    • 2019-05-23
    • 2013-04-18
    • 2015-01-21
    • 2021-12-19
    • 2018-08-26
    • 1970-01-01
    相关资源
    最近更新 更多