#15590: FileField path isn't settable
-------------------------------------+-------------------------------------
     Reporter:  simon29              |                    Owner:  simon29
         Type:  New feature          |                   Status:  new
    Component:  File                 |                  Version:  master
  uploads/storage                    |               Resolution:
     Severity:  Normal               |             Triage Stage:  Accepted
     Keywords:  filefield            |      Needs documentation:  1
  imagefield path filename           |  Patch needs improvement:  1
    Has patch:  1                    |                    UI/UX:  0
  Needs tests:  1                    |
Easy pickings:  0                    |
-------------------------------------+-------------------------------------

Comment (by claudep):

 I think that the OP use case is solved by setting the `name` instead of
 the `path` property (`name` being relative to the field storage base
 location), as demonstrated by the following test case:
 {{{
 diff --git a/tests/file_storage/tests.py b/tests/file_storage/tests.py
 index c3800cb..573914b 100644
 --- a/tests/file_storage/tests.py
 +++ b/tests/file_storage/tests.py
 @@ -560,6 +560,26 @@ class FileFieldStorageTests(unittest.TestCase):
          with temp_storage.open('tests/stringio') as f:
              self.assertEqual(f.read(), b'content')

 +    def test_filefield_name_updatable(self):
 +        """
 +        Test that the name attribute of a FileField can be changed to an
 existing file.
 +        """
 +        obj1 = Storage()
 +        obj1.normal.save("django_test.txt", ContentFile("content"))
 +
 +        initial_path = obj1.normal.path
 +        initial_name = obj1.normal.name
 +        new_path = initial_path.replace("test.txt", "test2.txt")
 +        new_name = initial_name.replace("test.txt", "test2.txt")
 +        # Rename the underlying file object
 +        os.rename(initial_path, new_path)
 +        obj1.normal.name = new_name
 +        obj1.save()
 +
 +        obj1 = Storage.objects.get(pk=obj1.pk)
 +        self.assertEqual(obj1.normal.name, new_name)
 +        self.assertEqual(obj1.normal.path, new_path)
 +

  # Tests for a race condition on file saving (#4948).
  # This is written in such a way that it'll always pass on platforms
 }}}

 If confirmed, this should be of course made clearer in the documentation.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/15590#comment:10>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/065.fabeff43f9bcfb1b1418bbd33a360066%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to