[Django] #31759: Why do instance hints in ForwardManyToOneDescriptor use the related object?

13 views
Skip to first unread message

Django

unread,
Jul 2, 2020, 12:20:37 PM7/2/20
to django-...@googlegroups.com
#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.

Django

unread,
Jul 7, 2020, 2:49:27 AM7/7/20
to django-...@googlegroups.com
#31759: Why do instance hints in ForwardManyToOneDescriptor use the related object?
-------------------------------------+-------------------------------------
Reporter: Daniel Miller | Owner: nobody
Type: Uncategorized | Status: closed
Component: Database layer | Version: 3.0
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

* status: new => closed
* resolution: => invalid


Comment:

Hiya. This kind of enquiry isn't really appropriate for the issue tracker.
Please see TicketClosingReasons/UseSupportChannels.

The is precisely the **other object** in the relation, so `value` for
`instance` and `instance` for `value`, allowing the router to adjust the
write DB depending on the related model.
Making your change, there's a failure in `tests/multiple_database`. I'd
guess digging through that would reveal more.

--
Ticket URL: <https://code.djangoproject.com/ticket/31759#comment:1>

Reply all
Reply to author
Forward
0 new messages