From a6687ed02561216c0f2a6dd86300cc75b0b1b063 Mon Sep 17 00:00:00 2001 From: Gagaro Date: Wed, 6 Nov 2024 15:34:27 +0100 Subject: [PATCH] Fixed #35890 -- Fixed custom pre_save in fields from parents models not being called. --- django/db/models/query.py | 2 +- tests/get_or_create/models.py | 4 ++++ tests/get_or_create/tests.py | 7 +++++-- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/django/db/models/query.py b/django/db/models/query.py index 21d5534cc9..851159bb90 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -992,7 +992,7 @@ class QuerySet(AltersData): # This is to maintain backward compatibility as these fields # are not updated unless explicitly specified in the # update_fields list. - for field in self.model._meta.local_concrete_fields: + for field in self.model._meta.concrete_fields: if not ( field.primary_key or field.__class__.pre_save is Field.pre_save ): diff --git a/tests/get_or_create/models.py b/tests/get_or_create/models.py index c5ba90a231..a07eab24a0 100644 --- a/tests/get_or_create/models.py +++ b/tests/get_or_create/models.py @@ -65,3 +65,7 @@ class Book(models.Model): db_column="publisher_id_column", ) updated = models.DateTimeField(auto_now=True) + + +class ComicBook(Book): + pass diff --git a/tests/get_or_create/tests.py b/tests/get_or_create/tests.py index 5128335f56..9fd0ab3884 100644 --- a/tests/get_or_create/tests.py +++ b/tests/get_or_create/tests.py @@ -12,6 +12,7 @@ from django.utils.functional import lazy from .models import ( Author, Book, + ComicBook, DefaultPerson, Journalist, ManualPrimaryKeyTest, @@ -579,12 +580,14 @@ class UpdateOrCreateTests(TestCase): def test_update_only_defaults_and_pre_save_fields_when_local_fields(self): publisher = Publisher.objects.create(name="Acme Publishing") - book = Book.objects.create(publisher=publisher, name="The Book of Ed & Fred") + book = ComicBook.objects.create( + publisher=publisher, name="The Book of Ed & Fred" + ) for defaults in [{"publisher": publisher}, {"publisher_id": publisher}]: with self.subTest(defaults=defaults): with CaptureQueriesContext(connection) as captured_queries: - book, created = Book.objects.update_or_create( + book, created = ComicBook.objects.update_or_create( pk=book.pk, defaults=defaults, )