我正在尝试创建包含多个图像的文章,但是我面临一个问题。
class Article(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
author = models.ForeignKey(User,on_delete=models.CASCADE,related_name='articles')
MediaImageArray = ArrayField(models.ImageField(upload_to='images',null=True,blank=True),size=8,null=True)
当我发布名为p1.jpg , p2.png
的图像列表时,它总是像这样返回null:
使用邮递员:
{
"id": "34d75f5e-bd82-45a1-9c86-10e3f260311d",
"author": "d39d29c8-4807-41c5-a3fa-e9cb117fdb2d",
"MediaImageArray": [
null,
null
],
}
有人知道原因吗?
答案 0 :(得分:0)
ArrayField
与存储图像(我认为甚至是文件)都不兼容。改用反向外键。
class Article(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
author = models.ForeignKey(User,on_delete=models.CASCADE,related_name='articles')
class ArticleImages(models.Model):
image = models.ImageField(upload_to='images',null=True,blank=True)
article = models.ForeignKey(Article, on_delete=models.CASCADE)