Changing name of file in FileField field

427 views
Skip to first unread message

Matthew Pava

unread,
Jun 9, 2020, 3:02:54 PM6/9/20
to Django users
Good day,
I have been struggling with this issue for weeks, and I can't figure out what I'm doing wrong.
I have a model with a FileField with a custom upload_to function. It seems to work fine when I'm doing runserver.
Problems arise during my tests.

My assertion error fails:
AssertionError: 'Rev0_2020-06-09_L123_My_Document_63ExUTF.docx' != 'Rev0_2020-06-09_L123_My_Document.docx'
- Rev0_2020-06-09_L123_My_Document_63ExUTF.docx
?                                 --------
+ Rev0_2020-06-09_L123_My_Document.docx


You see, it keeps adding these extra random characters to the filename, which is not at all in my upload_to function.

class DocumentTestCase(TestCase):
def create_document(self, **kwargs):
if 'file' not in kwargs:
kwargs['file'] = self.get_test_file()
return Document.objects.create(**kwargs)

     def _create_file(self):
f = tempfile.NamedTemporaryFile(suffix=self.TEST_FILE_EXTENSION, delete=False)
with open(f.name, mode='wb'):
            f.write(b"NA")
        return open(f.name, mode='rb')

TEST_FILE_EXTENSION = ".docx"

def setUp(self):
super().setUp()
        settings.MEDIA_ROOT = MEDIA_ROOT

def get_test_file(self):
file = self._create_file()
        return File(file, name=file.name)

    def test_rename_file_after_upload(self):
   
d1 = self.create_document(title="My Document", number="L123")
        title = d1.title.replace(" ", "_")
   
extension = Path(d1.file.path).suffix
        new_name = f"Rev{d1.revision}_{d1.revision_date}_{d1.number}_{title}{extension}"
   
self.assertEqual(Path(d1.file.name).name, new_name)


Chetan Ganji

unread,
Jun 9, 2020, 4:08:30 PM6/9/20
to django...@googlegroups.com
Hi,
I had come across a similar issue last year. Filename would change after the upload.
It been time and I am not sure why it happened then. But, i think it happened
because a file with the same name was already present the given location.
And the django/drf code would make the name of the file unique by appending random text like above.
You can check if this is the case.

You can try a couple of things.

1. Print the name of the file being generated as soon as it gets generated.  
2. Print the name of the file and Path(d1.file.name).name just before the assertion.
3. Check if a document with the same name already exists in the db and media root folder, and make sure that it doesnt.

I hope it helps.

Cheers


Regards,
Chetan Ganji
+91-900-483-4183


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/cbc812fc-6d5e-4afb-ab64-6bc0fad303a3o%40googlegroups.com.

matthe...@gmail.com

unread,
Jun 10, 2020, 2:57:37 PM6/10/20
to django...@googlegroups.com

Thank you so much for that! It was due to duplicate file names.

I had a tearDownClass method that was deleting my temp files after the test was completed. It should have been a tearDown method that occurred at the end of every test.

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/7bwtdfURlSk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAMKMUjsSKYU3FyD4SL%3DxumDc3spfM74KmnFhpRBbFniNe5ex2w%40mail.gmail.com.

Reply all
Reply to author
Forward
0 new messages