28

I'm a bit confused about which field to use. What I need is just a Field that will hold a file (audio and/or another one for an image)

The FileField seems to be specifically for uploading and I think it is a bit of overkill for what I need, I also don't like how it will automatically upload and rename the files to a unique filename (file_FHjgh758.txt) everytime something is uploaded.

The problem I have with the FilePathField is that it seems to require an absolute path, which would obviously be different from dev to production, I got around this by adding this to the model...

import os

class Foo(models.Model):

    path = os.path.dirname(os.path.dirname(__file__))
    path = os.join(path, 'media')
    audio = models.FilePathField(path=path)

I have no idea if this is safe or proper, and there aren't many examples online or in the docs to draw from.

To be clear, I just want to have a field which I can point to a file (audio or image) somewhere in my system. What would be the best way to go about doing this?

1 Answer 1

32

If you want to refer to files that are already on your filesystem rather than user uploaded files, then FilePathField is the one that you want.

In this regard a few comments:

  • Don't point to a path that is inside your app's source tree. That is not safe.

  • You can use settings or environment variables to handle development vs. production paths, e.g., put a FILE_PATH_FIELD_DIRECTORY setting in your development/production settings files and refer to this setting from your app:

    from django.conf import settings
    
    class Foo(models.Model):
        audio = models.FilePathField(path=settings.FILE_PATH_FIELD_DIRECTORY)
    
5
  • 2
    thanks for your answer, this makes sense to me although I am stuck on how to call the file location from a template. If I call the url saved in the db it is something like '/home/../../' and the html tries to make it relative to the dev server which is incorrect. I think I am missing something obvious here
    – Joff
    Oct 12, 2015 at 5:46
  • 6
    I think the issue is that FilePathField is meant more for files located on your server that are not accessible through the web server... if you want to public files then you are better off using FileField which has a url attribute and which will also save the files in an accessible place (in your MEDIA_ROOT). You can override the saving behaviour of the FileField to prevent the random suffix. Oct 12, 2015 at 7:16
  • 1
    Thanks for the followup, if I'm not mistaken I should also make an entry in the urls.py and a view to handle the requests coming into the /media folder? Is that correct or is that also dangerous?
    – Joff
    Oct 12, 2015 at 8:48
  • 2
    Django cannot handle requests for static files and media - it expects you to configure your web server to do that - see the extensive documentation on handling of static files. Your urls.py should only contain URLs for dynamic content generated by Django (except in development). Oct 12, 2015 at 8:53
  • Where the settings are placed? In which file? Jul 15, 2017 at 18:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.