Django FileField default file
15,170
You can specify the default file to use for that field as follows:
class Employer(models.Model):
logo = models.FileField(storage=FileSystemStorage(location=settings.MEDIA_ROOT), upload_to='logos', default='settings.MEDIA_ROOT/logos/anonymous.jpg')
Related videos on Youtube
Author by
brsbilgic
Updated on August 04, 2022Comments
-
brsbilgic almost 2 years
I have a model which contains
FileField
as belowclass Employer(models.Model): logo = models.FileField(storage=FileSystemStorage(location=settings.MEDIA_ROOT), upload_to='logos')
The question is how can I add a default file like "{{ MEDIA_ROOT}}/logos/anonymous.jpg" to this filefield ?
-
seanmus about 7 yearshow does the default parameter
settings.MEDIA_ROOT
get interpolated to the proper value in the string? -
Pedro Muñoz over 3 yearsthe path django is going to use in default is (by default): settings.MEDIA_ROOT + '/' + 'your_path', so be sure you place your default file there
-
ankush981 about 3 yearsDon't think this solution scales across environments? If the production instance runs S3, for example, does something like
MEDIA_ROOT
work? And I think the hardcodedFileSystemStorage
also doesn't work.