Advertisement
matrixd

changing image path after upload django

Sep 2nd, 2013
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from django.db import models
  2.  
  3. ...
  4.  
  5. class Picture(models.Model):
  6.    
  7.     ...
  8.  
  9.     pic = models.ImageField(upload_to='tmp')
  10.        
  11.     ...
  12.  
  13.     def save(self, *args, **kwargs):
  14.         #saving the picture first, because it must be uploaded and stored first
  15.         super(Picture, self).save(*args, **kwargs)
  16.         #moving picture if needed
  17.         if blablabla:
  18.             import os
  19.             from django.conf import settings
  20.             path = settings.MEDIA_ROOT + somedirname + '/'
  21.             if not os.path.exists(path):
  22.                 os.mkdir(path)
  23.             os.rename(self.pic.path, path + self.pic.name.split('/')[-1])
  24.             #self.pic is ImageFieldFile object
  25.             self.pic = somedir + '/' + self.pic.name.split('/')[-1]
  26.             #self.pic is string object
  27.             super(Picture, self).save(*args, **kwargs)
  28.             #now it's again an ImageFieldFile object with new path
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement