У меня есть две модели
class Place(models.Model):
title = models.CharField(max_length = 255)
description = models.TextField()
class PlaceImage(models.Model):
place = models.ForeignKey(Place, default=None, on_delete=models.CASCADE)
photo = models.ImageField(upload_to='places', verbose_name='Photo')
title = models.CharField(max_length = 250, default = "tmp")
и функция в представлении
def read_place(request, place_id):
place = Place.objects.filter(pk=place_id).annotate(photos=F('placeimage__photo')).values("id", 'title', 'photos')
return JsonResponse(list(place), safe=False)
, которое возвращает мне json объекты
[{"id": 1,
"title": "title1",
"photos": "places_upload_location/memorial1.jpg"},
{"id": 1,
"title": "title1",
"photos": "places_upload_location/memorial2.jpg"},
{"id": 1,
"title": "title1",
"photos": "places_upload_location/memorial3.jpg"}]
Но я бы хотел получить один объект Place с массивом фотографий, примерно так:
[{"id": 1,
"title": "title1",
"photos":
[{
"photo_id": 1,
"photo": "places_upload_location/memorial1.jpg"
"title": "photo 1"
}, {
"photo_id": 2,
"photo": "places_upload_location/memorial2.jpg"
"title": "photo 2"
}, {
"photo_id": 3,
"photo": "places_upload_location/memorial3.jpg"
"title": "photo 3"
}]
}]
Как я могу это сделать?