Opened 7 years ago

Closed 7 years ago

#6635 closed (invalid)

Unable to save model with ImageField/FileField on Windows

Reported by: anonymous Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Keywords: filefield windows
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Given the model, on Windows:

class StaffMember(models.Model):
    name = models.CharField(max_length=100, unique=True)
    photo = models.ImageField(upload_to="img/staff/", blank=True)
    #...

A validation error is raised whenever attempting to save from within the Django Admin interface. I found ticket #2923 and the issue seemed similar so I ran some tests on the code in django/db/models/fields/init.py. In the function "isWithinMediaRoot" "field_data" has a forward slash appended to the start of the path. On Windows "os.path.join" only returns the path after that leading forward slash resulting in an incorrect absolute path and a ValidationError. If you strip out the leading forward slash the absolute path is correctly determined.

Example (Python 2.5):

>>> root = 'F:\\root\\' # slash orientation doesn't matter here as far as os.path.join is concerned.
>>> path = '/path/to/file.txt'
>>> os.path.join(root, path)
'/path/to/file.txt'
>>> path = 'path/to/file.txt'
>>> os.path.join(root, path)
'F:\\root\\path/to/file.txt'

Attachments (1)

6635.patch (713 bytes) - added by justin.driscoll@… 7 years ago.
Strip forward slash from "field_data" in "isWithinMediaRoot".

Download all attachments as: .zip

Change History (3)

Changed 7 years ago by justin.driscoll@…

Strip forward slash from "field_data" in "isWithinMediaRoot".

comment:1 Changed 7 years ago by justin.driscoll@…

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Didn't mean to submit ticket anonymously.

comment:2 in reply to: ↑ description Changed 7 years ago by telenieko

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

Replying to anonymous:

class StaffMember(models.Model):
    name = models.CharField(max_length=100, unique=True)
    photo = models.ImageField(upload_to="img/staff/", blank=True)
    #...

In the function "isWithinMediaRoot" "field_data" has a forward slash appended to the start of the path.

The example Model you gave does not meet this condition (no forward slash at the begining)

Anyway, documentation says (crearly enough I think) that the upload_to field is under settings.MEDIA_ROOT, which means that it has to be *relative* to settings.MEDIA_ROOT, hence *not* starting with "/".

I understand this as an invalid ticket.

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