我需要获取带有相关图像的专辑。我在第一个循环abc和abc123 中有两个专辑, 在 abc 专辑中有两个图像,在 abc123 专辑中有 3 个图像,但我的反应是在每个专辑中返回 5 个图像。我无法做出正确的回应。
模型.py
class Upload(models.Model):
image=models.ImageField(upload_to='upload/', blank=True, null=True)
user = models.ForeignKey(User, on_delete=models.CASCADE)
# url= models.CharField(max_length=500)
def __str__(self):
return str(self.id)
class Album(models.Model):
name = models.CharField(max_length=128)
user = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return str(self.name)
class AlbumImage(models.Model):
album = models.ForeignKey(Album, on_delete=models.CASCADE)
image = models.ForeignKey(Upload, on_delete=models.CASCADE)
视图.py
def getalbum(self,request):
albums=Album.objects.filter(user=request.user)
album={}
image_list={}
all_results=[]
all_images=[]
for a in albums:
album_images=AlbumImage.objects.filter(album=a)
for i in album_images:
image_object=Upload.objects.get(id=str(i.image))
image_list=str(image_object.image)
all_images.append(image_list)
album[a.name]=all_images
all_results.append(album)
return Response(all_results)
回复
[
{
"abc": [
"upload/images_aPWBEcp.jpeg",
"upload/images.jpeg",
"upload/ic_id_card_placeholder_N0x2KP9.png",
"upload/images_jocZcBu.jpeg",
"upload/ic_id_card_placeholder_N0x2KP9.png"
],
"abc123": [
"upload/images_aPWBEcp.jpeg",
"upload/images.jpeg",
"upload/ic_id_card_placeholder_N0x2KP9.png",
"upload/images_jocZcBu.jpeg",
"upload/ic_id_card_placeholder_N0x2KP9.png"
]
}
]
abc专辑
上传/images_aPWBEcp.jpeg
上传/images.jpeg
abc123专辑
上传/ic_id_card_placeholder_N0x2KP9.png
上传/images_jocZcBu.jpeg
上传/ic_id_card_placeholder_N0x2KP9.png
慕虎7371278
慕无忌1623718
相关分类