Introduction
- Simple way to implement dynamic image upload_to path based on object.It is now possible for the
upload_toparameter of theFileFieldorImageFieldto 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):Description:
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)
imagepath is the callable (in this case a function). It simply gets the 'no' from the instance ofBooksand uses that in the upload path. Images will be uploaded to paths like: Book_Media/1/djano.jpg
Labels: Django
0 Comments:
Subscribe to:
Post Comments (Atom)