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
?
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With