Opened 17 years ago
Closed 16 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: | dev |
Severity: | Keywords: | filefield windows | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
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)
Change History (3)
by , 17 years ago
Attachment: | 6635.patch added |
---|
comment:2 by , 16 years ago
Resolution: | → invalid |
---|---|
Status: | new → 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.
Strip forward slash from "field_data" in "isWithinMediaRoot".