Skip to content

Instantly share code, notes, and snippets.

@hansek
Created March 16, 2017 09:10
Show Gist options
  • Save hansek/5e1d4b2bd5a84a8a139c8ed6e025b060 to your computer and use it in GitHub Desktop.
Save hansek/5e1d4b2bd5a84a8a139c8ed6e025b060 to your computer and use it in GitHub Desktop.
Django File mixin to automatically folderize uploaded file by app label and ID
from django.db import models
class FileAppIdFolderMixin(models.Model):
def upload_to(self, filename):
return '{app}/{id}/{filename}'.format(
app=self._meta.app_label,
id=self.id,
filename=filename
)
file = models.FileField(
upload_to=upload_to,
blank=True
)
class Meta:
abstract = True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment