[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.
* 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>