I'm working on a real estate app and want the listings to show only the first image of the Listing. Currently it is showing all images.
class Image(models.Model):
photo = models.ImageField(blank=True, upload_to=get_image_filename)
listing = models.ForeignKey(Listing, on_delete=models.CASCADE)
class ImageSerializerForListingList(serializers.ModelSerializer):
photo = serializers.ImageField()
class Meta:
model = Image
fields = ('photo', )
class ListingListSerializer(serializers.HyperlinkedModelSerializer):
image = ImageSerializerForListingList(many=True, required=False, source='image_set')
class Meta:
model = Listing
fields = ('url', 'address', 'image', )
def get_first_image(self, obj):
image = obj.image_set.all().first()
return ImageSerializerForListingList(image, many=True).data
This is still showing all images,
Any advice on this?