#31759: Why do instance hints in ForwardManyToOneDescriptor use the related
object?
-------------------------------------+-------------------------------------
Reporter: Daniel | Owner: nobody
Miller |
Type: | Status: new
Uncategorized |
Component: Database | Version: 3.0
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Why do the `instance` hints in `ForwardManyToOneDescriptor` use the
related object rather than the instance of the class being routed?
[https://github.com/django/django/blob/d36413a2eb5678dda35559a48769703acd784898/django/db/models/fields/related_descriptors.py#L223-L226
django/db/models/fields/related_descriptors.py:223]
{{{
if instance._state.db is None:
instance._state.db = router.db_for_write(instance.__class__,
instance=value)
if value._state.db is None:
value._state.db = router.db_for_write(value.__class__,
instance=instance)
}}}
I would expect that to read like so
{{{
if instance._state.db is None:
instance._state.db = router.db_for_write(instance.__class__,
instance=instance)
if value._state.db is None:
value._state.db = router.db_for_write(value.__class__, instance=value)
}}}
It looks like it may be a historical artifact from
[https://github.com/django/django/blob/acc095c333213eb1d978866ae5852cefe00ce4ef/django/db/models/fields/related.py#L285-L288
long ago], which was
[https://github.com/django/django/commit/1b3dc8ad9a28486542f766ff93318aa6b4f5999b
later converted] to what we see today.
The [https://docs.djangoproject.com/en/3.0/topics/db/multi-db/#topics-db-
multi-db-hints documentation on multi-db hints] is vague, it says the
instance is "an object instance that is related to the read or write
operation that is underway." While this is technically not inaccurate, it
seems non-intuitive to pass a related object rather than the object being
routed.
--
Ticket URL: <https://code.djangoproject.com/ticket/31759>
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/052.28835a349789d9091408fa5cf7f40431%40djangoproject.com.