From ec9ed07488aa46fa9d9fd70ed99e82fdf184632a Mon Sep 17 00:00:00 2001 From: Michael Scott Date: Tue, 20 Sep 2016 22:31:23 +0100 Subject: Fixed #27188 -- Allowed using unique=True with FileField. Thanks Tim Graham for the initial patch. --- tests/model_fields/models.py | 2 +- tests/model_fields/test_filefield.py | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) (limited to 'tests/model_fields') diff --git a/tests/model_fields/models.py b/tests/model_fields/models.py index aeeb6e88da..faa7d08189 100644 --- a/tests/model_fields/models.py +++ b/tests/model_fields/models.py @@ -216,7 +216,7 @@ class DataModel(models.Model): class Document(models.Model): - myfile = models.FileField(upload_to='unused') + myfile = models.FileField(upload_to='unused', unique=True) ############################################################################### # ImageField diff --git a/tests/model_fields/test_filefield.py b/tests/model_fields/test_filefield.py index e67b6dd515..3fefcd1fb6 100644 --- a/tests/model_fields/test_filefield.py +++ b/tests/model_fields/test_filefield.py @@ -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()) -- cgit v1.3