Skip to main content

dj-upload-to

Travis CI build status Code coverage percentage Latest version on PyPI Wheel Status Supported Python versions

Synopsis

Small application that simplifies naming of uploaded files. License is MIT.

Installation

You can get dj-upload-to release version from pypi with pip:

$ pip install dj-upload-to

or development one from github:

$ pip install -e git+https://github.com/marazmiki/dj-upload-to#egg=dj-upload-url

You’re not need include it into your INSTALLED_APPS

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 see, UploadTo generates callable object (with __call__ methodfor passing into upload_to attribute of FileField (see django upload_to docs for details)

When you save model with image originally named myphoto.JPG, file

will be saved with name such as:

model/ab/cd/abcdabcd-0123-4567-8901-234567890ab.jpg

where:

  • model is prefix automatically generated from model class. You can override it

  • ab is the first 2 char segment of filename

  • cd is the second 2 char segment of filename

  • abcdabcd-0123-4567-8901-234567890ab autogenerated with uuid filename

  • .jpg is a lower cased extension taken from original file

Customize

You can customize behavior of UploadTo with options in constructor:

  • prefix: prefix of filename. Default is dj_upload_to.not_provided. If None, 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

CHANGELOG

1.1

  • Add deconstruct() support

1.0.2

  • Fix sad setup.py bug

1.0.1

  • Add prefix disabling

  • Add changelog

  • Updated docs

1.0.0

Initial release

Metadata

Release files for dj-upload-to 1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distributions (sdists)

Source distribution for dj-upload-to 1.1
File Size Uploaded
dj-upload-to-1.1.zip 8.9 kB Details
dj-upload-to-1.1.tar.gz 4.1 kB Details
dj-upload-to-1.1.tar.bz2 4.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for dj-upload-to 1.1
File Interpreter ABI Platform
dj_upload_to-1.1-py3-none-any.whl Python 3 none any Details

Total release size: 23.4 kB

Release files / dj-upload-to-1.1.zip

Download URL dj-upload-to-1.1.zip
Size 8.9 kB
Tags Source
SHA-256 checksum
How to use checksums
005275f0321af7146371fded4039857fca4b1b6a67c5f8c0e3fed8bf3677dba2
BLAKE2b-256 checksum
How to use checksums
38da13722de752289fb38fdadd6e7fea0d0799a2eaef692782a8d4765011e6a0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / dj-upload-to-1.1.tar.gz

Download URL dj-upload-to-1.1.tar.gz
Size 4.1 kB
Tags Source
SHA-256 checksum
How to use checksums
ef9794c9022d6b3f0499a0cbce6f680427180a1ee95e05da0bde636c8fa309a3
BLAKE2b-256 checksum
How to use checksums
25968f2e029d4706e0b09321da52cad014fb8d0d1b631ed761e0f64e988bd5b8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / dj-upload-to-1.1.tar.bz2

Download URL dj-upload-to-1.1.tar.bz2
Size 4.4 kB
Tags Source
SHA-256 checksum
How to use checksums
fe6321220e8822ad03c70027a8c83f7ee4bd3076642b6b5b8c1c30c370131e2f
BLAKE2b-256 checksum
How to use checksums
14913c3034695404eddce61ff1935574e060bd248562b6935d8c417d01096f86
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / dj_upload_to-1.1-py3-none-any.whl

Download URL dj_upload_to-1.1-py3-none-any.whl
Size 6.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
85c4c599b06b3ba2b23d22748a759286a8aa10c230aa35cecf90aea0a237043f
BLAKE2b-256 checksum
How to use checksums
505582ebae108388c4356072de944a39d43bbfb7eb9a378092e6c7a9f17f3cd7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

1.1 This release

4 release files

1.0.2

4 release files

1.0.1

1.0.0

4 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page