Get absolute file path of FileField of a model instance in Django
Asked Answered
O

1

38

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?

Orthman answered 16/12, 2017 at 6:21 Comment(0)
O
77

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

Orthman answered 16/12, 2017 at 6:31 Comment(3)
Can you provide official documentation link?Dorm
@decadenza, check deconstruct method for FileField class here docs.djangoproject.com/en/2.2/_modules/django/db/models/fields/…Nicolas
Maybe this will help: docs.djangoproject.com/en/3.0/topics/files/…Jacquard

© 2022 - 2024 — McMap. All rights reserved.