我需要获取具有相关图像的相册。我在第一循环 abc 和 abc123 中有两个相册,在abc相册中有两个图像,在abc123相册中有3个图像,但是我的回应是每张专辑返回5张图片。 我无法建立适当的回应。
models.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)
views.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 相册
upload / images_aPWBEcp.jpeg
upload / images.jpeg
abc123 相册
upload / ic_id_card_placeholder_N0x2KP9.png
upload / images_jocZcBu.jpeg
upload / ic_id_card_placeholder_N0x2KP9.png
答案 0 :(得分:3)
这里:
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
您要在for循环之前初始化all_images
并继续附加到该循环,因此两个相册最终都共享相同的图像列表。修复很容易:在for循环中初始化all_images
:
for a in albums:
all_images=[]
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
话虽这么说,您正在使它变得比必须的复杂得多,并且您没有正确使用django的功能。不用手动创建AlbumImage
模型,而是使用you could use a M2MField
instead并避免了手动创建列表等的需要(并且获得了更好的性能,也使得ORM可以优化查询)。
答案 1 :(得分:2)
我认为您需要将循环内的all_images列表清空为:
def getalbum(self,request):
albums=Album.objects.filter(user=request.user)
album={}
image_list={}
all_results=[]
for a in albums:
album_images=AlbumImage.objects.filter(album=a)
all_images = []
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)
答案 2 :(得分:0)
因为'all_images'是一个列表,而list在Python中是可变的。在内部循环中,当您添加“ all_images”列表时,无论引用到何处,它也会更新。同样,这也不是获取所需信息的正确方法。了解Django中的序列化器,并在这种情况下使用它们。 https://www.django-rest-framework.org/api-guide/serializers/