我在Django models.py中有以下内容(模型被剥离到只有必要的字段)
class Product(BaseProduct):
price = models.IntegerField()
productfoto = models.ManyToManyField("ProductFoto", related_name="%(app_label)s_%(class)s_related")
class Meta:
abstract = True
ordering = ['name']
# Inherits Product class
class ConsumerProduct(Product):
categorie= models.ForeignKey(Categorie)
class Meta:
verbose_name_plural = "ConsumerProducten"
class ProductFoto(models.Model):
myimage= FileBrowseField("Image", max_length=200, directory='producten')
,这在admins.py中:
class ProductFotoInline(admin.TabularInline):
extra = 1
model = Product.productfoto.through
class ConsumerProductAdmin(admin.ModelAdmin):
prepopulated_fields = {"slug": ("name",)}
inlines= [ProductFotoInline]
admin.site.register(ConsumerProduct, ConsumerProductAdmin)
请注意以下事项:
我会说这应该可行,但是在尝试添加新的ConsumerProduct时我收到以下ImproperlyConfigured错误:
'model' is a required attribute of 'ConsumerProductAdmin.inlines[0]'.
感谢任何帮助
答案 0 :(得分:0)
我决定让我的Product类非抽象。通过admin删除此类的注册(删除行admin.site.register(Product)
)
您不会在后端看到它,但是在您的数据库中,它会更麻烦,因为您将同时拥有Product
和ConsumerProduct
表