1
0
mirror of https://github.com/django/django.git synced 2025-10-23 21:59:11 +00:00

Fixed #27188 -- Allowed using unique=True with FileField.

Thanks Tim Graham for the initial patch.
This commit is contained in:
Michael Scott
2016-09-20 22:31:23 +01:00
committed by Tim Graham
parent 625cd5bcb3
commit ec9ed07488
7 changed files with 23 additions and 34 deletions

View File

@@ -4,6 +4,7 @@ import unittest
from django.core.files import temp
from django.core.files.uploadedfile import TemporaryUploadedFile
from django.db.utils import IntegrityError
from django.test import TestCase, override_settings
from .models import Document
@@ -61,6 +62,15 @@ class FileFieldTests(TestCase):
Document.objects.create(myfile='something.txt')
self.assertEqual(Document.objects.defer('myfile')[0].myfile, 'something.txt')
def test_unique_when_same_filename(self):
"""
A FileField with unique=True shouldn't allow two instances with the
same name to be saved.
"""
Document.objects.create(myfile='something.txt')
with self.assertRaises(IntegrityError):
Document.objects.create(myfile='something.txt')
@unittest.skipIf(sys.platform.startswith('win'), "Windows doesn't support moving open files.")
# The file's source and destination must be on the same filesystem.
@override_settings(MEDIA_ROOT=temp.gettempdir())