my model:
class myfile(models.model): file = models.filefield(upload_to="myfiles", max_length=500) slug = models.slugfield(max_length=500, blank=true)
when file names contain special chars, such ' '(space), special chars replaced underscore automatically. where (in function) happen? how disable automatic validation?
thanks
update
any comments on following codes? thanks
""" https://docs.djangoproject.com/en/1.9/_modules/django/core/files/storage/#storage.get_valid_name overwrite get_valid_name() function, """ class overwritestorage(filesystemstorage): def get_valid_name(self, name): print "name=", name return name class myfile(models.model): file = models.filefield(upload_to="myfiles", max_length=500, storage=overwritestorage())
it depends on storage. filefield
calls storage.get_valid_name
on sotrage. ref
you can overwrite function (depending on storage), think it's better leave is. can use name field.
if use filesystemstorage
, call django.utils.text.py
, replaces spaces underscores. ref
edit:
assuming use default filesystemstorage
here's how override it:
create file (probably in main app)
storage.py:
from django.core.files.storage import filesystemstorage django.utils.deconstruct import deconstructible @deconstructible class customfilesystemstorage(filesystemstorage): def get_valid_name(self, name): return name
(you might need @deconstructible
decorator migrations)
now have 2 options use storage. can either specify in models explicitly:
my model:
class myfile(models.model): file = models.filefield(upload_to="myfiles", max_length=500, storage=customfilesystemstorage) slug = models.slugfield(max_length=500, blank=true)
or can set globally in settings.py
:
default_file_storage = '{yourapp}.storage.customfilesystemstorage'
Comments
Post a Comment