Skip to content

How do I rename image that I uploaded through Django Rest Framework #8808

Discussion options

You must be logged in to vote

Solution:

To rename the filename that we are uploaded using drf

def image_dir_path(instance, filename):
extension = filename.split('.')[-1]
new_filename = "pimages/candidate_%s.%s" % (instance.name, extension)
return new_filename

Create your models here.

class ResumeModel(models.Model):
name = models.CharField(max_length=100)
email = models.EmailField()
dob = models.DateField(auto_now=False, auto_now_add=False)
state = models.CharField(choices=STATE_LIST, max_length=50)
gender = models.CharField(choices=GENDER_LIST, max_length=20)
location = models.CharField(max_length=100)
# pimage = models.ImageField(upload_to='pimages', blank=True)
pimage = models.ImageField(upload_to=image_dir_path, b…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by niraj876
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant