Rnice4christ
Rnice4christ

Reputation: 78

Django ImageField Model: Get file path on upload_to callable

I have a Django admin field for uploading images, in my model I'm using a callable to get the FileField instance and Filename. My goal is to open the file the user is trying to upload and upload it to my Dropbox account through their API and return the URL where the image is saved. How do I get the full path for the file being uploaded? Here is what I currently have in my model:

    def upload_to_dropbox(self, filename):
      return DropboxStorage.store_file(self.image, filename)

    image = models.ImageField(upload_to=upload_to_dropbox)

I tried using the ImageField's .url property but that returns my MEDIA_ROOT path, maybe I'm going about this the wrong way?

Upvotes: 0

Views: 797

Answers (1)

Alexander Brill
Alexander Brill

Reputation: 96

The upload_to field is only for setting the folder and filename. If you want to talk with the Dropbox API on storing files you need to create a Dropbox implementation of the Storage api and use the storage keyword instead.

Django-storages already have a dropbox implementation: http://django-storages.readthedocs.io/en/latest/backends/dropbox.html

from storages.backends.dropbox import DropBoxStorage
image = models.ImageField(storage=DropBoxStorage())

Upvotes: 1

Related Questions