Opened 7 years ago

Closed 7 years ago

Last modified 2 years ago

#10410 closed Uncategorized (worksforme)

FileField saves one filename on disk and another on DB

Reported by: Elland Owned by: peterbe
Component: File uploads/storage Version:
Severity: Normal Keywords: FileField valid filename
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


When saving a file with spaces on the name, it will be saved with underscores for spaces on disk, but the database will point to it without underscores,
but with the regular spaces, leading to a File Not Found error.

Change History (11)

comment:1 Changed 7 years ago by jacob

  • milestone changed from 1.2 to 1.1
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 7 years ago by russellm

  • Component changed from Uncategorized to File uploads/storage

comment:3 Changed 7 years ago by peterbe

  • Owner changed from nobody to peterbe
  • Status changed from new to assigned

comment:4 Changed 7 years ago by peterbe

Unable to reproduce. Currently on revision 10189.

When uploading a file called "my file.txt" it gets saved on the filesystem as "my_file.txt" and in SQL it's stored as "./my_file.txt".
Thus, there's no reference back to "my file.txt".

Also I don't know how to actually reproduce the "leading to a File Not Found error". What does that mean? How are you *viewing* the FileField

comment:5 Changed 7 years ago by peterbe

  • Resolution set to invalid
  • Status changed from assigned to closed

This test proves that the bug is not reproducable

from django.db import models
class MyModel(models.Model):
    my_file = models.FileField(upload_to='.')

import os
from django.test import TestCase
from django.conf import settings
from models import MyModel
class SimpleTest(TestCase):

    def test_file_upload(self):

        # create a fake file to upload
        instance = MyModel.objects.create()
            open('upload me.txt','w').write('any junk\n')
            from django.core.files import File
            f = File(open('upload me.txt'))
            settings.MEDIA_URL = ''
  'with spaces.txt', f)

            # django replaces all the spaces with underscores
            assert os.path.isfile('with_spaces.txt')
            # but in the database it will be 'with spaces.txt'
            self.assertEqual(instance.my_file, u'./with_spaces.txt')
            self.assertEqual(instance.my_file.url, './with_spaces.txt')
            os.remove('upload me.txt')
            if os.path.isfile('with_spaces.txt'):

I think what this proves is that there's no problem with "with spaces.txt" once the file has been uploaded.

comment:6 Changed 7 years ago by anonymous

  • Resolution invalid deleted
  • Status changed from closed to reopened

I can reproduce this bug. It also exists if there is a file with the same name. If you upload two images with the same name, then when you try to show the first image, the second image is shown. (because the second image get a '_' appended to it, but the name in the database does not)

I wrote a bug report here:
I originally thought it was specific to django-storages, so I submitted a bug report to django-storages:

comment:7 Changed 7 years ago by russellm

  • Resolution set to worksforme
  • Status changed from reopened to closed

peterbe has contributed an executable test case that implements the problem case described in the original ticket, and that test case passes on Django Trunk under SQLite, Postgres and MySQL.

I don't doubt that you are experiencing a problem, but at this point, the onus is on you to establish how your error case differs from the working test case that we have. We can't fix any bug that we can't reproduce.

comment:8 Changed 7 years ago by rent

Where in the code-base does Django write to the database? I want to see if I can trace is myself, to see what I have done wrong.

comment:9 Changed 5 years ago by jacob

  • milestone 1.1 deleted

Milestone 1.1 deleted

comment:10 Changed 2 years ago by m1ndpixel

  • Easy pickings unset
  • Severity set to Normal
  • Type set to Uncategorized
  • UI/UX unset
Last edited 2 years ago by m1ndpixel (previous) (diff)

comment:11 Changed 2 years ago by collinanderson

Is this still an issue now that upload_to is optional?

Note: See TracTickets for help on using tickets.
Back to Top