Search code examples
djangoadminimagefield

Use ImagField from Django to save image paths in different directory other than MEDIA_ROOT


I have created a Model that should only be accessed by the admin user and I would like to user the admin interface to manage theinstances I create. So basically this will be used to generate static.

One of the fields I would like to use is the ImageField that saves references to image in the MEDIA_ROOT. However, I would prefer to have those images references inside the STATIC_ROOT instead of MEDIA_ROOT in order not to have them mixed up with user generated data.

Is it possible to set this without hardcoding? Thanks in advance

Pedro


Solution

  • You can do it by defining custom file storage:

    from django.core.files.storage import FileSystemStorage
    
    image_store = FileSystemStorage(location='/tmp/images')
    
    class YourModel(models.Model):
          image = models.ImageField(storage=image_store)