How to Manually Create Django-Filer Files

2014-04-07 | #python, #solution, #webdev

Django-Filer has a sublime admin integration, but tells us nothing about possibilities to manually create files from a frontend for example, which is a somewhat strange omission. After some fiddling with the source I came up wiht the following lines of code to achieve this arcane magic:

from filer.models.imagemodels import Image

from filer.models.foldermodels import Folder

from filer import settings as filer_settings

from filer.utils.loader import load_object

def create_filer_image(upload, folder=None, owner=None):

filename =

for filer_class in filer_settings.FILER_FILE_MODELS:

FileSubClass = load_object(filer_class)

if FileSubClass.matches_file_type(filename, upload, None):

FileForm = modelform_factory(

model = FileSubClass,

fields = ('original_filename', 'owner', 'file')



uploadform = FileForm({'original_filename': filename, 'owner': owner}, {'file': upload})

if uploadform.is_valid():

file_obj =

if not isinstance(file_obj, Image):

raise ValidationError('the given file was no image')

if folder is not None:

folder_chain = []

for index, folder_part in enumerate(filter(None, '{folder}'.format(folder=folder).split('/'))):

parent_id = None if index == 0 else folder_chain[-1:][0].pk

if folder_part in ('1', '2', '3', '4', '5', '6', '7', '8', '9',):

folder_part = '0{part}'.format(part=folder_part)


folder_obj = Folder.objects.get(name=folder_part, parent_id=parent_id, level=index)

except Folder.DoesNotExist:

folder_obj = Folder() = folder_part

folder_obj.level = index

folder_obj.owner_id = owner

folder_obj.parent_id = parent_id


file_obj.folder_id = None if len(folder_chain) == 0 else folder_chain[-1:][0].pk


file_obj.folder_id = None

file_obj.is_public = filer_settings.FILER_IS_PUBLIC_DEFAULT

render_icons = file_obj.icons

render_thumnails = file_obj.thumbnails


raise ValidationError('file info didn\'t validate')

return file_obj