Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 429
Kotlinboy Avatar asked Dec 16 '17 06:12

Kotlinboy


People also ask

How do I find absolute path?

You can determine the absolute path of any file in Windows by right-clicking a file and then clicking Properties. In the file properties first look at the "Location:" which is the path to the file.

How do I get the full path of an uploaded file in Python?

To get current file's full path, you can use the os. path. abspath function. If you want only the directory path, you can call os.

What is file absolute path?

An absolute path always contains the root element and the complete directory list required to locate the file. For example, /home/sally/statusReport is an absolute path. All of the information needed to locate the file is contained in the path string.


1 Answers

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

like image 149
Kotlinboy Avatar answered Oct 24 '22 09:10

Kotlinboy