Django FileField() disable upload - django

I have a FileField() in a Django model. On save(), I intercept this and upload the actual file data to a remote-backend/Bucket, then save the URL returned from the service in another field.
The file actually uploads to my file-system (I believe).
How can I disable the FileField() to not actually upload the file to wherever media is stored with Django?

You could use upload_to atttribute of FileField.
Reference:
https://docs.djangoproject.com/en/2.2/ref/models/fields/#django.db.models.FileField.upload_to

Related

How does Django store the reference of media file (or images) in database

My question is not How to store file/image or fetch/show/access them into the templates.
My question is one level deeper, I want to know
When we define a file/image field in Django model, and upload a file to it which store the file/image into media root. Then we access through modelInstance.file.url . So does Django stores url of file/image or name or just location in media root ( .url will give this appended to media url) into the database.
I just want to know what reference does Django save into database for media file??
generally it saves the location of file, inside media root. you can see in database also.
here's official docs: https://docs.djangoproject.com/en/3.0/topics/files/

How to Download BinaryField in django admin?

I am working on a legacy system, where an institution's logo field is declared as a BinaryField, I was able to upload the file using the file input widget, how can I edit the registry to be able to download the file? As with FileField.
I use this for upload file add_file_upload

Is there any way to associate file upload with a particular model in loopback 3?

I am uploading file on this /api/containers/container/upload but I want to upload file using /api/Profile/upload. Is this possible ? Is there any way to automatically associate uploading file name with field created on Profile model ?
For example, if I am uploading file name "abc.txt", it gets automatically uploaded to server and it also inserts/updates field "image_name" on Profile model.
I am new to loopback Pls help.
-
-
-
You could do that by adding 'after remote method' hook, or 'after save' hook, so after upload and before the response is returned (or after your container was saved) you could process it and do something, like for example saving the profile picture. You can find more about that here and here.

Get absolute file path of FileField of a model instance in Django

I have a page where people can upload files to my server.
I want to do something with the file in Celery. So, I need to know the absolute filepath of the uploaded FileFiled of my Model.
Let's say I queried the model and got the instance.
Now I need to get the absolute file path including the filepath.
obj = Audio.objects.get(pk=1)
I'm currently trying obj.filename and it's only printing the file name and not the absolute path.
I know I can get the upload path I input into upload_to and media directory, but I was wondering if there was a more DRY and automatic approach.
How do I get the absolute path of file which is a file filed in obj?
Found an answer.
I gotta do a .path on the FileField
If I do
obj.audio_file.path
obj is the model instance I queried and audio_file is the filefield

More Blobstore upload woes with standard Django

I'm implementing an image upload feature for my Django app (plain Django 1.4 , NOT the non-rel version) running on Google App Engine. The uploaded image is wrapped in a Django model which allows the user to add attributes like a caption and search tags.
The upload is performed by creating a Blobstore upload url through the function call blobstore.create_upload_url(url). The function argument is the url to which the Bobstore redirects when the upload is complete. I want this to be the url of the default Django form handler that performs the save/update of the model that wraps the image so I don't have to duplicate default Django behaviour for form validation, error reporting and database update.
I tried supplying reverse('admin:module_images_add') to create_upload_url() but this doesn't work as it throws an [Errno 30] Read-only file system exception. I presume this originates from the default Django form handler again trying to upload the file the standard Django way but then hits the brick wall of Google App Engine not allowing access to the file system.
At the moment, the only way I can see to get this working without duplicating code is by strictly separating processes: one for defining an image model instance and the second for uploading the actual image. Not very intuitive.
See also this question and answer which I posted earlier.
Any suggestions on how to get this working using one form and reusing Django default form handlers?
EDIT:
I've been reading up on decorators (I'm relatively new to Python) and from what I read, decorators appear to able to modify the behaviour of existing Python code. Would it be possible to change the runtime behaviour of the existing form handler to solve the above using a decorator? I obviously have to (1) develop the decorator and (2) attach it to the default handler. I'm not sure if (2) is possible as it has to be done runtime. I cannot patch the Django code running on GAE...
Well, I finally managed to get this working. Here's what I did in case anyone runs into this as well:
(1) I removed the ImageFile attribute from my model. It ended up causing Django to try and do a file upload from the file system which is not allowed in GAE.
(2) I added a Blobstore key to my model which is basically the key to the GAE BlobStore blob and is required to be able to serve the image at a later stage. On a side note: this attribute has limited length using the GAE SDK but is considerably longer in GAE production. I ended up defining a TextField for it.
(3) Use storage.py with Daniel Roseman's adaption from this question and add the BlobstoreFileUploadHandler to the file handlers in your SETTINGS.PY. It will ensure that the Blobstore key is there in the request for you to save with your model.
(4) I created a custom admin form which contains an ImageField named "image". This is required as it allows you to pick a file. The ImageField is actually "virtual" as its only purpose on the form is to allow me to pick a file for uploading. This is crucial as per (1).
(5) I overwrote render_change_form() method of my ModelAdmin class which will prepare a Blobstore upload url. The upload url has two versions: one for adding new images and one saving changes to existing. Upload urls are passed to the template via the context object.
(6) I modified the change_form.html to include the Blobstore upload url from (5) as the form's action.
(7) I overwrote the save_model() method of my ModelAdmin:
def save_model(self, request, obj, form, change):
if request.FILES.has_key("blobkey"):
blob_key = request.FILES["blobkey"].blobstore_info._BlobInfo__key
obj.blobstore_key = blob_key
super(PhotoFeatureAdmin, self).save_model(request, obj, form, change)
This allows me to retrieve the blob key as set by the upload handler and set it as a property of my model.
For deletion of image models, I added a special function which is triggered by the delete signal of the model. This will keep the Blobstore in sync with the image models in the app.
That's it. The above allows to upload images to the blob store of GAE where each blob is neatly wrapped in a Django model object which admin users can maintain. The good thing is that there's no need to duplicate standard Django behaviour and the model object of the image can easily be extended with attributes in the future.
Final word: in my opinion the support for blobs in plain Django on GAE is currently very poor considering the above. It should be much easier to achieve this, without having to rely on Django non-rel code and a rather long list of modifications; alternatively Google should state something about this in their developer documents. Unless I missed something, this is undocumented territory.