dj-upload-to
Synopsis
This application help you don't care about file naming when you're uploading
files into your Django Project. Just add upload_to=UploadTo()
keyword argument
to ``FileField``s and ``ImageField``s in your models and forget about
code duplicity.
If you don't like default behaviour, you can easily customize it.
License is MIT.
Installation
Just get dj-upload-to latest release version from PyPI with pip command:
(.virtualenv) $ pip install dj-upload-to
Also you could use last development version. To do that, use repository URL instead of package name:
$ pip install -e git+https://github.com/marazmiki/dj-upload-to#egg=dj-upload-url
Unlike most of django applications, you shouldn't include dj_upload_to into the INSTALLED_APPS of your settings file.
Usage
Assumes you have model:
from django.db import models
from dj_upload_to import UploadTo
upload_to = UploadTo()
class Model(models.Model):
file = models.ImageField(upload_to=upload_to)
As you can see UploadTo
generates callable object (with __call__
method for
passing into upload_to
attribute of FileField (see
Django's upload_to docs for details)
And now when you will save model with image originally named myphoto.JPG
,
just uploaded file will be saved with name like that:
abcdabcd-0123-4567-8901-234567890ab.jpg
Please note: file extention became lowercase. And storage path will be like that:
model/ab/cd/abcdabcd-0123-4567-8901-234567890ab.jpg
where:
-
model
is prefix automatically generated as model class name in lowercase. Of course, you could override this behaviour; -
ab
is the first 2-char segment of safe and trusted filename; -
cd
is the second 2-char segment of filename; -
abcdabcd-0123-4567-8901-234567890ab.jpg
is the safe and trusted filename generated by uorselves.
Customize
You can customize behavior of UploadTo
with options in constructor:
-
prefix
: prefix of filename. Default isdj_upload_to.not_provided
. IfNone
, prefix will be missed. If not explicitly set, will be generated automatically based on model class name -
num_seg
: number of parts of segmentation. Default is 2 -
seg_size
: length of segment in chars. Default is 2 -
save_name
: use original name without autogeneration. Default is False
There are some examples:
>>> model_instance = Model()
>>> # Disable prefix
>>> UploadTo(prefix=None)(model_instance, 'file.jpg')
u'c0/17/c01745b4-e70b-4dd8-a5f7-76fec32fcb83.jpg'
>>> # Explicitly given prefix
>>> UploadTo(prefix='my_files')(model_instance, 'file.jpg')
u'my_files/d9/a4/d9a4ef25-11b0-41bb-a543-baaac6553024.jpg'
>>> # Four segment and automatically generated prefix
>>> UploadTo(num_seg=4)(model_instance, 'file.jpg')
u'model/36/52/99/f6/365299f6-8dc5-4ca2-848d-965f002a9b72.jpg'
>>> # Segment length is 4 chars
>>> UploadTo(seg_size=4)(model_instance, 'file.jpg')
u'model/3142/f2ef/3142f2ef-2680-4a99-82fc-3c8d9d3179dc.jpg'
>>> # Save original filename
>>> UploadTo(save_name=True)(model_instance, 'file.jpg')
u'model/file.jpg'
>>> # Save original filename without prefix
>>> UploadTo(save_name=True, prefix=None)(model_instance, 'file.jpg')
u'file.jpg'
Contributing
Ideas, bugfixes, pull requests are welcome on GitHub