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')
Share:
15,170

Related videos on Youtube

brsbilgic
Author by

brsbilgic

Updated on August 04, 2022

Comments

  • brsbilgic
    brsbilgic almost 2 years

    I have a model which contains FileField as below

    class 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
    seanmus about 7 years
    how does the default parameter settings.MEDIA_ROOT get interpolated to the proper value in the string?
  • Pedro Muñoz
    Pedro Muñoz over 3 years
    the 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
    ankush981 about 3 years
    Don'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 hardcoded FileSystemStorage also doesn't work.