美好的一天,不久前,与 Django 一起工作。遇到以下问题。
有两种型号
class Product(models.Model):
productID = models.AutoField(primary_key=True)
brandID = models.ForeignKey(Brand, verbose_name=u'Бренд')
categoryID = models.ForeignKey(Category, verbose_name=u'Категория')
productPhotos = models.ManyToManyField("ProductPhoto", verbose_name = "Фотографии товара", related_name="photos", blank=True)
class ProductPhoto(models.Model):
productPhotoID = models.AutoField(primary_key=True)
productID = models.ForeignKey(Product)
imageFile = models.FileField(u'Путь к изображение', upload_to=makeUploadPath)
dateModified = models.DateField(auto_now=True)
对于管理员,请使用以下 TabularInline
class ProductPhotoInline(admin.TabularInline):
model = ProductPhoto
verbose_name = u"Фото"
verbose_name_plural = u"Фотографии"
class ProductAdmin(admin.ModelAdmin):
list_display = ('title')
search_fields = ...
fieldsets = ...
)
inlines = [ ProductPhotoInline, ]
问题是在表 ProdcutPhoto 中添加产品时 productId 字段被填满,而表 Products productPhotos 中的字段为空。如何最好地填写这两个字段。