Created
March 16, 2017 09:10
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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