Blogger Template by Blogcrowds.


  • Simple way to implement dynamic image upload_to path based on object.It is now possible for the upload_to parameter of the FileField or ImageField to be a callable, instead of a string. The callable is passed the current model instance and uploaded file name and must return a path.
class Books(models.Model):
class Collection(models.Model):
       def imagepath(instance,filename):
            val="Book_Media/" + str( + "/" + str(filename)
return val
  • imagepath is the callable (in this case a function). It simply gets the 'no' from the instance of Books and uses that in the upload path. Images will be uploaded to paths like: Book_Media/1/djano.jpg


Post a Comment

Newer Post Older Post Home