#35890: pre_save field in parent models are not called during update in
update_or_create
-------------------------------------+-------------------------------------
     Reporter:  Gagaro               |                    Owner:  (none)
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  dev
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Description changed by Gagaro:

Old description:

> With the following models:
>
> {{{
> class Parent(models.Model):
>     modified_at = models.DateTimeField(auto_now=True)
>

> class Child(Parent):
>     modified_at_child = models.DateTimeField(auto_now=True)
> }}}
>
> When calling `update_or_create`, the pre_save of the `modified_at` field
> is not called, and the field is not updated:
>

> {{{
> # Create
>
> >>> instance, _ = Child.objects.update_or_create(pk=1)
> >>> instance.modified_at
> datetime.datetime(2024, 11, 6, 14, 1, 11, 52881,
> tzinfo=datetime.timezone.utc)
> >>> instance.modified_at_child
> datetime.datetime(2024, 11, 6, 14, 1, 11, 54363,
> tzinfo=datetime.timezone.utc)
>

> # Update
>
> >>> instance, _ = Child.objects.update_or_create(pk=1)
> >>> instance.modified_at
> datetime.datetime(2024, 11, 6, 14, 1, 11, 52881,
> tzinfo=datetime.timezone.utc)
> >>> instance.modified_at_child
> datetime.datetime(2024, 11, 6, 14, 1, 21, 517245,
> tzinfo=datetime.timezone.utc)
> }}}
>

> The regression seems to have happened in
> https://github.com/django/django/commit/6cc0f22a73970dd7c0d29d4d8d2ff9e1cc862b30
>
> Using `self.model._meta.concrete_fields` instead of
> `self.model._meta.local_concrete_fields` seems to solve the issue, but I
> don't know the internal API well enough to understand all of the
> implications.
>
> I'm also preparing a PR with a unit test and the fix, I'll update this
> ticket when it will be ready.

New description:

 With the following models:

 {{{
 class Parent(models.Model):
     modified_at = models.DateTimeField(auto_now=True)


 class Child(Parent):
     modified_at_child = models.DateTimeField(auto_now=True)
 }}}

 When calling `update_or_create`, the pre_save of the `modified_at` field
 is not called, and the field is not updated:


 {{{
 # Create

 >>> instance, _ = Child.objects.update_or_create(pk=1)
 >>> instance.modified_at
 datetime.datetime(2024, 11, 6, 14, 1, 11, 52881,
 tzinfo=datetime.timezone.utc)
 >>> instance.modified_at_child
 datetime.datetime(2024, 11, 6, 14, 1, 11, 54363,
 tzinfo=datetime.timezone.utc)


 # Update

 >>> instance, _ = Child.objects.update_or_create(pk=1)
 >>> instance.modified_at
 datetime.datetime(2024, 11, 6, 14, 1, 11, 52881,
 tzinfo=datetime.timezone.utc)
 >>> instance.modified_at_child
 datetime.datetime(2024, 11, 6, 14, 1, 21, 517245,
 tzinfo=datetime.timezone.utc)
 }}}


 The regression seems to have happened in
 
https://github.com/django/django/commit/6cc0f22a73970dd7c0d29d4d8d2ff9e1cc862b30

 Using `self.model._meta.concrete_fields` instead of
 `self.model._meta.local_concrete_fields` seems to solve the issue, but I
 don't know the internal API well enough to understand all of the
 implications.

 PR : https://github.com/django/django/pull/18777

--
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35890#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/0107019301e88c2c-37bdfda2-6926-4394-85b0-c1e675059913-000000%40eu-central-1.amazonses.com.

Reply via email to