Opened 5 years ago

Closed 5 years ago

Last modified 3 years ago

#10410 closed (worksforme)

FileField saves one filename on disk and another on DB

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


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.

Attachments (0)

Change History (9)

comment:1 Changed 5 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 5 years ago by russellm

  • Component changed from Uncategorized to File uploads/storage

comment:3 Changed 5 years ago by peterbe

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

comment:4 Changed 5 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 5 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 5 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 5 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 5 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 3 years ago by jacob

  • milestone 1.1 deleted

Milestone 1.1 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'

E-mail address and user name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.