Blogger Template by Blogcrowds.

Introduction

  • 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.

Models.py
class Books(models.Model):
       no=models.IntegerField()
class Collection(models.Model):
       def imagepath(instance,filename):
            val="Book_Media/" + str(instance.Books.no) + "/" + str(filename)
return val
      Page=models.ForeignKey('Books')
      image=models.ImageField(upload_to=imagepath)
Description:
  • 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

0 Comments:

Post a Comment



Newer Post Older Post Home