[Python] How to make ImageField returns relative path?

Discussão em 'Python' iniciado por Stack, Abril 7, 2025.

  1. Stack

    Stack Membro Participativo

    I don't know why, but my ImageFields return full URL pathes.

    This is MEDIA_URL setting:

    MEDIA_URL = '/cc/cc/media/'


    I use this custom FileStorage:

    class ASCIIFileSystemStorage(FileSystemStorage):
    def get_valid_name(self, name):
    return str(uuid.uuid4()) + '.' + name.split('.')[-1]


    And simple ModelSerializers (from django rest framework) without overwriting smth like that for model Module1Item:

    class Module1Item(models.Model):
    ...
    background = models.ImageField(blank=True)


    class Module1ItemSerializer(serializers.ModelSerializer):
    class Meta:
    model = Module1Item
    fields = '__all__'


    What can be the reason of those behaviour? I need relative URLs.

    Continue reading...

Compartilhe esta Página