你把一对夫妻的关系搞错了。形象应该是产品的外键。这样,一个产品可以有多个图像。
class Image(models.Model):
image = models.ImageField()
imageMetaKeyWords = models.CharField("Meta keywords for SEO", max_length = 255,
help_text = "Comma delimited words for SEO")
imageMetaDescription = models.CharField("Meta description", max_length = 255,
help_text = "Content for image meta tag description")
defaultImage = models.BooleanField(default= False)
product = models.ForeignKey("Product", on_delete=models.CASCADE, related_name="images")
#if you want to get the product's images you can call product.images
并删除以下内容:
productImages = models.ForeignKey(Image, on_delete=models.CASCADE)
迁移,然后在你的管理员。py:
from django.contrib import admin
from .models import Product, Image
from .forms import ProductAdminForm
# Register your models here.
class ImageTabularInline(admin.TabularInline):
model = Image
class ProductAdmin(admin.ModelAdmin):
form = ProductAdminForm
inlines = [ImageTabularInline,]
class Meta:
model = Product
admin.site.register(Image)
admin.site.register(Product, ProductAdmin)