1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

Fixed #35384 -- Raised FieldError when saving a file without a name to FileField.

This commit is contained in:
Jonny Park
2024-04-28 11:54:54 +09:00
committed by Sarah Boyce
parent b691accea1
commit c0b0ce85ed
3 changed files with 39 additions and 2 deletions

View File

@@ -5,13 +5,14 @@ import tempfile
import unittest
from pathlib import Path
from django.core.exceptions import SuspiciousFileOperation
from django.core.exceptions import FieldError, SuspiciousFileOperation
from django.core.files import File, temp
from django.core.files.base import ContentFile
from django.core.files.uploadedfile import TemporaryUploadedFile
from django.db import IntegrityError, models
from django.test import TestCase, override_settings
from django.test.utils import isolate_apps
from django.utils.version import PY311
from .models import Document
@@ -72,6 +73,27 @@ class FileFieldTests(TestCase):
with self.assertRaisesMessage(SuspiciousFileOperation, msg):
document.save()
def test_save_content_file_without_name(self):
d = Document()
d.myfile = ContentFile(b"")
msg = "File for myfile must have the name attribute specified to be saved."
with self.assertRaisesMessage(FieldError, msg) as cm:
d.save()
if PY311:
self.assertEqual(
cm.exception.__notes__, ["Pass a 'name' argument to ContentFile."]
)
def test_delete_content_file(self):
file = ContentFile(b"", name="foo")
d = Document.objects.create(myfile=file)
d.myfile.delete()
self.assertIsNone(d.myfile.name)
msg = "The 'myfile' attribute has no file associated with it."
with self.assertRaisesMessage(ValueError, msg):
getattr(d.myfile, "file")
def test_defer(self):
Document.objects.create(myfile="something.txt")
self.assertEqual(Document.objects.defer("myfile")[0].myfile, "something.txt")