mirror of
https://github.com/django/django.git
synced 2026-02-09 02:49:25 +08:00
Fixed #36847 -- Ensured auto_now_add fields are set on pre_save().
Regression in 94680437a4. Refs #27222.
During INSERT operations, `field.pre_save()` is called to prepare values
for db insertion. The `add` param must be `True` for `auto_now_add`
fields to be populated. The regression commit passed `False`, causing
`auto_now_add` fields to remain `None` when used by other fields, such
as `upload_to` callables.
Thanks Ran Benita for the report.
This commit is contained in:
committed by
nessita
parent
2831eaed79
commit
fe189dc43a
@@ -262,10 +262,20 @@ class DataModel(models.Model):
|
||||
# FileField
|
||||
|
||||
|
||||
def upload_to_with_date(instance, filename):
|
||||
return f"{instance.created_at.year}/{filename}"
|
||||
|
||||
|
||||
class Document(models.Model):
|
||||
myfile = models.FileField(storage=temp_storage, upload_to="unused", unique=True)
|
||||
|
||||
|
||||
# See ticket #36847.
|
||||
class DocumentWithTimestamp(models.Model):
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
myfile = models.FileField(storage=temp_storage, upload_to=upload_to_with_date)
|
||||
|
||||
|
||||
###############################################################################
|
||||
# ImageField
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ from django.db import IntegrityError, models
|
||||
from django.test import TestCase, override_settings
|
||||
from django.test.utils import isolate_apps
|
||||
|
||||
from .models import Document
|
||||
from .models import Document, DocumentWithTimestamp
|
||||
|
||||
|
||||
class FileFieldTests(TestCase):
|
||||
@@ -209,3 +209,9 @@ class FileFieldTests(TestCase):
|
||||
|
||||
document = MyDocument(myfile="test_file.py")
|
||||
self.assertEqual(document.myfile.field.model, MyDocument)
|
||||
|
||||
def test_upload_to_callable_sees_auto_now_add_field_value(self):
|
||||
d = DocumentWithTimestamp(myfile=ContentFile(b"content", name="foo"))
|
||||
d.save()
|
||||
self.assertIsNotNone(d.created_at)
|
||||
self.assertIs(d.myfile.name.startswith(f"{d.created_at.year}/foo"), True)
|
||||
|
||||
Reference in New Issue
Block a user