我有这个models.py代码,我想从一个字段中获取多个值/文件,而不必创建另一个仅用于图像的表。
当前我只是重复这些字段
class contentimage(models.Model):
content = models.ForeignKey(element, on_delete=models.CASCADE)
title = models.CharField(max_length=200)
titleimg1 = models.CharField(max_length=200)
image1 = models.ImageField(blank=True, upload_to='media/')
img1description = models.TextField()
titleimg2 = models.CharField(max_length=200)
image2 = models.ImageField(blank=True, upload_to='media/')
img2description = models.TextField()
titleimg3 = models.CharField(max_length=200)
image3 = models.ImageField(blank=True, upload_to='media/')
img3description = models.TextField()
titleimg4 = models.CharField(max_length=200)
image4 = models.ImageField(blank=True, upload_to='media/')
img4description = models.TextField()
titleimg5 = models.CharField(max_length=200)
image5 = models.ImageField(blank=True, upload_to='media/')
img5description = models.TextField()
最佳输出将是django admin中的输入字段,其中加号可以添加多个图像 如果有人可以向正确的方向指引我,那将非常有帮助。
答案 0 :(得分:0)
将图像制成内联。首先给他们自己的模型课:
class Image(models.Model):
content = models.ForeignKey(contentimage, on_delete=PROTECT). # this is what connect the two models
title = models.CharField(max_length=200)
image = models.ImageField(blank=True, upload_to='media/')
description = models.TextField()
然后在管理员中注册该课程:
class ImageInlineAdmin(admin.InlineModelAdmin):
extra = 0
model = Image
然后用一行代码在您的contentimage管理员中注册内联:
inlines = [ImageInlineAdmin]