how do i rename images that i uploaded through django rest framework?

What you want to do is handle the filename modification at model level, so that any uploads from your custom views, django admin and even django-rest-framework will be handled the same.

From your snippets, it looks like you’re trying to do this, but I don’t where rename_and_path is defined.

So define the following method above your Shop model


def logo_dir_path(instance, filename):
    extension = filename.split('.')[-1]
    og_filename = filename.split('.')[0]
    new_filename = "shop_%s.%s" % (instance.name, extension)

    return new_filename

Then on your logo field on the model, add the following attribute:

logo = models.ImageField(upload_to=logo_dir_path)

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top