![](/img/trans.png)
[英]I couldn't find any solutions for my TypeError: object of type 'bool' has no len() error
[英]TypeError: object of type 'bool' has no len() i cant find problem
我得到 TypeError: object of type 'bool' has no len in django admin
楷模
模型.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
管理員.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
i try to save object in django admin and i get this error but create object with python shell has no problem
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.