Search code examples
djangofilepathabsolute-pathfilefield

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?


Solution

  • 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