0

Is there any update on image field serializer? I was trying to server an image api to the frontend. My serializer is not returning image link instead its returning the path relative to the base dir of the django-project

models.py

def upload_to(instance, filename):
    return 'profiles/{filename}'.format(filename=filename)

class Picture(models.Model):
    image = models.ImageField(upload_to=upload_to)

serializer

class PictureSerializer(serializers.ModelSerializer):
    class Meta:
        model = Picture
        fields = ['image']

**serializer.data result **

[
    {
        "image": "/media/profiles/quiz-app-.jpg"
    },
    {
        "image": "/media/profiles/coding2.jpg"
    },
    {
        "image": "/media/profiles/dummy_2s9CyV8.jpg"
    }
]

instead of the above result I want something like a link in return

[
    {
        "image": "http://127.0.0.1:8000/api/media/profiles/quiz-app-.jpg"
    },
    {
        "image": "http://127.0.0.1:8000/api/media/profiles/coding2.jpg"
    },
    {
        "image": "http://127.0.0.1:8000/api/media/profiles/dummy_2s9CyV8.jpg"
    }
]

1 Answers1

0

You can check for details

How can I get the full/absolute URL (with domain) in Django?

Using following project can help to solve this problem using external file stores and CDNs for better performance in future.

https://django-storages.readthedocs.io/en/latest/

Vikram Patil
  • 628
  • 6
  • 20