#35125: Parent model fields are ignored when preparing the update_fields
-------------------------------------+-------------------------------------
Reporter: Gustavo | Owner: nobody
Silva |
Type: | Status: new
Uncategorized |
Component: Database | Version: 4.2
layer (models, ORM) | Keywords: models,
Severity: Normal | update_or_create
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Hi all,
This might be an edge case but I'd like to report this functionality that
might be slightly broken, as someone coming from Django 3.X to Django 4.X.
According to
[https://docs.djangoproject.com/en/4.2/ref/models/fields/#django.db.models.DateField.auto_now
4.2 the docs], using `auto_now` in model fields might not result in actual
updates after the `save()` method is called.
However, the code has a piece for backward compatibility, where these
fields are indeed picked up:
https://github.com/django/django/blob/4.2.3/django/db/models/query.py#L959-962
This raises the first problem, which is the inconsistency between
documentation and the actual code. I appreciate the backward compatibility
though, it's a lifesaver for my specific use case :)
The other issue I am having is with a potentially unusual model scheme
where I have parent classes for some models. These are not abstract
classes, but sorts of aggregator classes:
{{{
class A(model.Model):
updated_at = models.DateTimeField(auto_now=True)
something = models.BooleanField(default=True)
# in another app
class B(A):
name = models.CharField()
}}}
Previously, when we did `B.objects.update_or_create(name="x",
defaults={"something": False})` it would automatically update `updated_at`
timestamp. In 4.2 (and above, apparently) this is no longer happening.
**However**, if `updated_at` was a field in the `B` class, `updated_at`
would be picked up for update with the same `update_or_create` call.
Notice I do not need to specify the `updated_at` field.
My workaround for now will be to override the save method of the parent
classes to include the `updated_at` field in the `update_fields` list.
[https://github.com/django/django/blob/4.2.3/django/db/models/query.py#L963
when line 963 of the query] is called it only uses the local model fields
to prepare the new `update_fields`. This drops any field coming from the
parent class.
----
I'd like to propose to use `self.model._meta._non_pk_concrete_field_names`
instead of `self.model_meta.local_concrete_fields` in the above line of
code, or any other attribute that contains the parent fields that its
better suited. If this is accepted as an issue, I can create a patch and
submit it for review/approval.
Django version: 4.2.3
Python: 3.11.1
This is the first time I am contributing to Django - I apologise if there
is a better way to report issues or if there are any templates to follow.
Feel free to ask any question or ask for what is not clear in this
description.
Thanks in advance.
--
Ticket URL: <https://code.djangoproject.com/ticket/35125>
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 on the web visit
https://groups.google.com/d/msgid/django-updates/0107018d1cf5bc26-6a4690e9-14b5-4175-a011-d6194971daa9-000000%40eu-central-1.amazonses.com.