How to save a Filefield template using a preexisting file?

Asked

Viewed 199 times

4

I am importing a set of preexisting files to Django, and would like to refer them to my templates (which they use FileField or ImageField). For example, one of my models is like this:

class Imagem(models.Model):
    arquivo = ImageField(upload_to=u"img/%Y/%m/%d")
    miniatura = ImageField(upload_to=u"img/thumb/%Y/%m/%d")
    ...

And I have several images (already copied to my MEDIA_ROOT) thus:

MEDIA_ROOT
|- gallery
   |-album1
     |-foto1.jpg
     |-foto2.jpg
     |-...
     |-thumbs
       |-thumbs_foto1.jpg
       |-thumbs_foto2.jpg
       |-...

(Note that the images are not under the folder upload_to, but in a completely different)

Is it possible to create instances of this model by referencing the preexisting images, without having to make copies of them and without having to rename and/or move them? And how would be the command to create these instances?

I have no problems navigating the file system, finding the names of the images and associating each image with its thumbnail, my problem is how to create the template itself:

path_imagem = os.path.join(settings.MEDIA_ROOT, 'gallery/album1/foto1.jpg')
path_miniatura = os.path.join(settings.MEDIA_ROOT, 'gallery/album1/thumbs/thumbs_foto1.jpg')
Imagem.objects.create(???)

Examples in the Django documentation (e.g..: FileField, "File Uploads", "Managing Files") are all related to new files, where one creates a File or if you get one from an upload, for example. In my case the files already exist and are already under the MEDIA_ROOT.

1 answer

3


Yes, it is possible to make these references as long as the files are inside the MEDIA_ROOT. Just pass the way relative as an argument for the objects.create:

path_imagem = 'gallery/album1/foto1.jpg'
path_miniatura = 'gallery/album1/thumbs/thumbs_foto1.jpg'
Imagem.objects.create(imagem=path_imagem, miniatura=path_miniatura, ...)

And then the model will reference the file correctly, disregarding the upload_to.

Important detail: if an absolute path is passed instead of a relative one, the method will not work, even though the file is inside the MEDIA_ROOT!

path_imagem = os.path.join(settings.MEDIA_ROOT, 'gallery/album1/foto1.jpg')
path_miniatura = os.path.join(settings.MEDIA_ROOT, 'gallery/album1/thumbs/thumbs_foto1.jpg')
i = Imagem.objects.create(imagem=path_imagem, miniatura=path_miniatura)

print(i.imagem.path) # /var/www/vhosts/example.com/media/gallery/album1/foto1.jpg
                     # Aparentemente ok, mas...

print(i.imagem.url) # /var/www/vhosts/example.com/media/gallery/album1/foto1.jpg
                    # Deveria ser relativo ao MEDIA_URL, e não ao MEDIA_ROOT, ex.:
                    # /media/gallery/album1/foto1.jpg
                    # https://example.com/media/gallery/album1/foto1.jpg
                    # etc

You need a relative path for the URL to be mounted correctly.

Source: Set an Imagefield path in Django Manually

Browser other questions tagged

You are not signed in. Login or sign up in order to post.