Reputation: 21
I'm pulling my hair out with this. When I attempt to add a 'Product' from my admin page, I'm getting an IntegrityError: main_product.img may not be 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)
I was using Pillow to resize images when being upload, so I had a custom save() function in my ProductImages model. I removed that thinking it was the problem, but it's still not working. As you can tell, I'm really new to Django and Python. Any and all help appreciated.
Edit: forgot to mention that I've added blank=true and null=true to Product.img, then used South to migrate the table.
Edit 2: This is my new ProductImages model.
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)
I used South and ran:
python manage.py schemamigration main --auto
python manage.py migrate main
Still having the error. If I were to add the img field to my Products model, how would I be able to add multiple imgs in the admin panel?
Upvotes: 1
Views: 3665
Reputation: 692
Actually you need to add null=True
in the img field, So make it img = models.ImageField(upload_to='images', blank=True, null=True)
.
The difference is that because blank=True
decides that whether the fields will be required by the forms. blank=False
means that field cannot be blank and blank=True
means field will not be required.
Regarding null=True
sets NULL in column of your DB which will solve your issue.
Upvotes: 2