It's a fairly and obvious choice to make:
This is what Django docs say about FilePathField
A CharField whose choices are limited to the filenames in a certain directory on the filesystem. Has some special arguments, of which the first is required:
If you want to serve files on your server go for FilePathField
.If, on the other hand, you want to store files received from users, then, go for the FileField
.
Update:
As for storing the file, whether the file is coming from other users or machines: if you are using Django ModelForms
, you can deal with your file as follows (assuming you are working with Django forms and not DRF
serializers):
from django.http import HttpResponseRedirectfrom django.shortcuts import renderfrom .forms import UploadFileForm# Imaginary function to handle an uploaded file.from somewhere import handle_uploaded_filedef upload_file(request): if request.method == 'POST': form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): handle_uploaded_file(request.FILES['file']) form.save() # Explaination below return HttpResponseRedirect('/success/url/') else: form = UploadFileForm() return render(request, 'upload.html', {'form': form})
When you issue form.save()
your file in that form will be saved into your MEDIA_ROOT
. You can define an alternative path for storage in your FileField
.
You can postpone the decision for downloading the file later given that it's a separate issue. You are not forced to make the decision here or provision anything complicated in advance.