However, if the instance is then later used (accidentally or occasionally,
that would defeat the point of the optimisation), the value of
`MyModel.relatedfield` can be incorrect. I think inefficient would be a
better default than incorrect, because the mistake is subtle.
I imagine the `ForwardManyToOneDescriptor` could check the primary key of
the cache value being returned against the primary key on the model
instance and if there is a mismatch, then the cache value should be
deleted and a new query should be made.
{{{
>>> instance = MyModel(relatedfield_id=1)
>>> print(instance.relatedfield.pk)
1
>>> instance.relatedfield_id = 2
>>> print(instance.relatedfield.pk)
1
>>> instance.save()
>>> print(instance.relatedfield.pk)
1
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/27710>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* component: Uncategorized => Database layer (models, ORM)
* type: Uncategorized => Cleanup/optimization
* stage: Unreviewed => Accepted
Comment:
I thought there might be another ticket where this was proposed and
possibly rejected as "too magical", but I'm not going to do a search now.
The idea seems reasonable at first glance.
--
Ticket URL: <https://code.djangoproject.com/ticket/27710#comment:1>
* owner: nobody => Paulo
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/27710#comment:2>
* has_patch: 0 => 1
Comment:
PR: https://github.com/django/django/pull/9122
--
Ticket URL: <https://code.djangoproject.com/ticket/27710#comment:3>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"ee49306176a2d2f1751cb890bd607d42c7c09196" ee493061]:
{{{
#!CommitTicketReference repository=""
revision="ee49306176a2d2f1751cb890bd607d42c7c09196"
Fixed #27710 -- Made Model.save() invalidate cached, stale relations after
a primary key assignment.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/27710#comment:4>