![](/img/trans.png)
[英]Peewee ORM gives IntegrityError: user_id may not be NULL
[英]IntegrityError: may not be NULL
我正在用这个拔头发。 当我尝试从管理页面添加“产品”时,出现IntegrityError:main_product.img可能不为NULL。
Models.py
class Product(models.Model):
name = models.CharField(max_length=500)
short = models.CharField(max_length=250)
description = models.TextField()
price = models.DecimalField(max_digits=8, decimal_places=2)
in_stock = models.BooleanField()
def __unicode__(self):
return self.name
class ProductImages(models.Model):
product = models.ForeignKey(Product, blank=True)
img = models.ImageField(upload_to='images', blank=True)
caption = models.CharField(max_length=300, blank=True)
class ProductFeatures(models.Model):
product = models.ForeignKey(Product)
feature = models.CharField(max_length=500)
Admin.py
class ProductFeaturesAdmin(admin.TabularInline):
model = ProductFeatures
extra = 1
class ProductImageAdmin(admin.TabularInline):
model = ProductImages
extra = 1
class ProductAdmin(admin.ModelAdmin):
list_display = ('name', 'price', 'in_stock')
inlines = [ProductFeaturesAdmin, ProductImageAdmin]
admin.site.register(Product,ProductAdmin)
我在上载时使用Pillow调整图像大小,因此在ProductImages模型中有一个自定义的save()函数。 我删除了以为是问题的想法,但仍然无法正常工作。 如您所知,我真的是Django和Python的新手。 任何和所有帮助表示赞赏。
编辑 :忘记提及我已经向Product.img添加blank = true和null = true,然后使用South来迁移表。
编辑2:这是我的新ProductImages模型。
class ProductImages(models.Model):
product = models.ForeignKey(Product)
img = models.ImageField(upload_to='images', blank=True, null=True)
caption = models.CharField(max_length=300, blank=True)
我用南方跑了:
python manage.py schemamigration main --auto
python manage.py migrate main
仍然有错误。 如果要将img字段添加到“产品”模型中,如何在管理面板中添加多个img?
实际上,您需要在img字段中添加null=True
,因此将其设置为img = models.ImageField(upload_to='images', blank=True, null=True)
。 区别在于,因为blank=True
决定表单是否需要该字段。 blank=False
表示该字段不能为空, blank=True
表示该字段将不是必需的。 关于null=True
将在数据库的列中设置NULL,这将解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.