Django assigning a remote file to a FileField without download and reuploading


I'm using django-storages as my default file storage. I have a script that uploads videos directly from the client to google cloud storage. Is there any way to associate this file to a FileField without downloading and reuploading the file. Thank you for any help!


Answer

A FileField field is just a pimped CharField, usually persisting a path (depending on the storage). Find out what path should be stored for your uploaded files (e.g. from other files uploaded through your app). You can get raw values using values_list:

Model.objects.filter(pk=known_instance.pk).values_list('file_field_name')

and set it directly for your manually uploaded files:

instance.file_field_name = 'required/raw/value/for/resource'
instance.save()