How can I change the URL that points to a File on the FileInput widget in the Django Admin frontend to point to my view

In my django project I want the user to be able to upload files, that are only accessible to users from the same company and from users that are staff. The solution I found is, that I upload the Files to a custom FileSystemStorage with a base_url that is not in urls, so files are not accessible over that url, the only way to access them is through my view.

The problem is, that in the django admin frontend on a FileInput widget, it links to the url that is not accessible. How can I link it to my view?

This is my model:

upload_storage = FileSystemStorage(location='/private/', base_url='/private/')


class Datei(models.Model):
    datei = models.FileField(upload_to='vertraege', storage=upload_storage)
    ...

This is my view:

@login_required(login_url='/login/')
def serve_file(request, file_id):
    file = Datei.objects.get(id=file_id)
    user = User.objects.get(id=request.user.id)
    if file.firma == user.firma or user.is_staff:
        return FileResponse(file.datei)
    else:
        return HttpResponseForbidden()
Back to Top