2👍
✅
You can use a SerializerMethodField
to return the list of associated images:
class PackageGalleryDetailSerializer(serializers.ModelSerializer):
images = serializers.SerializerMethodField()
class Meta:
model = Package
fields = '__all__'
def get_images(self, package):
return [
# Change this to meet your need e.g. `gallery.image.url`
gallery.image.name
for gallery in package.gallery.all()
]
Also, the related_name
from PackageGallery
to Package
should be named e.g. galleries
to have a better notion of the relationship as it’s a many-to-one.
And you can drop the get_queryset
method from ListPackageGallery
as we don’t need any filtering.
Source:stackexchange.com