This is accurate. However, there are other database-altering methods, such
as create(), for which the cache ought to be cleared but isn't. This
results in this confusing state of affairs:
{{{
x = MyModel.objects.prefetch_related("related_objects").get()
assert len(x.related_objects.all()) == 0
x.related_objects.create()
assert len(x.related_objects.all()) == 1 # Assertion fails because the
prefetch cache isn't cleared by create()
}}}
Using add() rather than create() would cause the above code to work as
expected. If there's a good reason for create() not clearing the cache,
could that be documented please? Otherwise, could it clear the cache? From
a search of Stack Overflow, this is definitely a source of confusion for
some.
--
Ticket URL: <https://code.djangoproject.com/ticket/34633>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Comment (by David Sanders):
The original ticket #26706, [https://groups.google.com/d/topic/django-
developers/ejOpJ_r7tv8/discussion ML thread] &
[https://github.com/django/django/pull/6725 PR] didn't seem to make any
mention of `create()`
I'd agree this seems inconsistent but keen to see what felix & charettes
(charettes reviewed the PR) think 🤔
--
Ticket URL: <https://code.djangoproject.com/ticket/34633#comment:1>