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.
From: django-users@googlegroups.com <django-users@googlegroups.com> On Behalf Of Chetan Ganji Sent: Tuesday, June 9, 2020 3:07 PM To: django-users@googlegroups.com Subject: Re: Changing name of file in FileField field 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 <http://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 <mailto:ganji.che...@gmail.com> ganji.che...@gmail.com <http://ryucoder.in/> http://ryucoder.in On Wed, Jun 10, 2020 at 12:31 AM Matthew Pava <matthew.p...@gmail.com <mailto:matthew.p...@gmail.com> > wrote: 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 <http://f.name> , mode='wb'): f.write(b"NA") return open(f.name <http://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 <http://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) -- 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+unsubscr...@googlegroups.com <mailto:django-users+unsubscr...@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 <https://groups.google.com/d/msgid/django-users/cbc812fc-6d5e-4afb-ab64-6bc0fad303a3o%40googlegroups.com?utm_medium=email&utm_source=footer> . -- 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+unsubscr...@googlegroups.com <mailto:django-users+unsubscr...@googlegroups.com> . To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAMKMUjsSKYU3FyD4SL%3DxumDc3spfM74KmnFhpRBbFniNe5ex2w%40mail.gmail.com <https://groups.google.com/d/msgid/django-users/CAMKMUjsSKYU3FyD4SL%3DxumDc3spfM74KmnFhpRBbFniNe5ex2w%40mail.gmail.com?utm_medium=email&utm_source=footer> . -- 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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/002c01d63f58%24d66e40b0%24834ac210%24%40gmail.com.