1

I stored image file in path djangor_prject_name/pictures/image_name.jpg by using

models.ImageField(upload_to='pictures/services/', max_length=255, null=True, blank=True)

and file is uploaded successfully. But while retrieving, what is the exact url to view image? Because id Database, the value is just

picutes/services/image_name.jpg

So, in my view file, I am using vue.js and need full path or url of the image to display.

1 Answer 1

3

After creation it won't give you full url, but in the list it will come with full url. So for that you use custom serializers.SerializerMethodField.

class TestSerializer(serializers.ModelSerializer):
    photo = serializers.SerializerMethodField()

    class Meta:
        model = Test
        fields = ('photo',) 

    def get_photo(self, car):
        request = self.context.get('request')
        photo = Test.photo.url
        return request.build_absolute_uri(photo)

For further details refer here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.