diff --git a/django/db/models/fields/files.py b/django/db/models/fields/files.py index 184a8dbd69..c4c44f18f8 100644 --- a/django/db/models/fields/files.py +++ b/django/db/models/fields/files.py @@ -157,9 +157,7 @@ class FileDescriptor(object): def __get__(self, instance=None, owner=None): if instance is None: - raise AttributeError( - "The '%s' attribute can only be accessed from %s instances." - % (self.field.name, owner.__name__)) + return self # This is slightly complicated, so worth an explanation. # instance.file`needs to ultimately return some instance of `File`, diff --git a/tests/file_storage/tests.py b/tests/file_storage/tests.py index 8ff3959427..6b02073bbf 100644 --- a/tests/file_storage/tests.py +++ b/tests/file_storage/tests.py @@ -18,6 +18,7 @@ from django.core.files.storage import FileSystemStorage, get_storage_class from django.core.files.uploadedfile import ( InMemoryUploadedFile, SimpleUploadedFile, TemporaryUploadedFile, ) +from django.db.models.fields.files import FileDescriptor from django.test import ( LiveServerTestCase, SimpleTestCase, TestCase, override_settings, ) @@ -447,9 +448,7 @@ class FileFieldStorageTests(TestCase): return 255 # Should be safe on most backends def test_files(self): - # Attempting to access a FileField from the class raises a descriptive - # error - self.assertRaises(AttributeError, lambda: Storage.normal) + self.assertIsInstance(Storage.normal, FileDescriptor) # An object without a file has limited functionality. obj1 = Storage()