[Django] #31803: Should ModelState.field_cache be documented?

8 views
Skip to first unread message

Django

unread,
Jul 20, 2020, 8:55:12 AM7/20/20
to django-...@googlegroups.com
#31803: Should ModelState.field_cache be documented?
-----------------------------------------+------------------------
Reporter: Jaap Roes | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 3.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
While writing a model method that relies on data of a related object I
decided that it would be nice if this method could log a warning when it
is called without the related object having been prefetched.

I came across this StackOverflow question
https://stackoverflow.com/questions/36402129/is-there-a-way-to-check-
whether-a-related-object-is-already-fetched and based on that I
implemented something like the following:

{{{
def my_method(self):
if 'related_object' not in self._state.fields_cache:
warnings.warn(
'Calling MyModel.my_method without prefetching the related
object can inefficient '
'Consider adding prefetch_related/select_related to the
originating queryset.',
category=RuntimeWarning,
stacklevel=3
)
...
}}}

This seems to work as intended.

Checking the docs I see that `Model._state` ''is'' documented
(https://docs.djangoproject.com/en/3.0/ref/models/instances/#state), but
the `fields_cache` attribute ''isn't''.

Can I rely on `fields_cache`? Should I consider it to be public but
undocumented, or private with the possibility of disappearing without
notice?

--
Ticket URL: <https://code.djangoproject.com/ticket/31803>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jul 20, 2020, 9:02:21 AM7/20/20
to django-...@googlegroups.com
#31803: Should ModelState.field_cache be documented?
-------------------------------------+-------------------------------------

Reporter: Jaap Roes | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Documentation | Version: 3.0
Severity: Normal | Resolution:

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 Jaap Roes):

* type: Uncategorized => Cleanup/optimization
* component: Uncategorized => Documentation


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

Django

unread,
Jul 21, 2020, 12:41:41 AM7/21/20
to django-...@googlegroups.com
#31803: Should ModelState.field_cache be documented?
-------------------------------------+-------------------------------------

Reporter: Jaap Roes | Owner: nobody
Type: | Status: closed

Cleanup/optimization |
Component: Documentation | Version: 3.0
Severity: Normal | Resolution: wontfix

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 felixxm):

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


Comment:

IMO `ModelState.field_cache` is an implementation detail and shouldn't be
documented. There is an **internal** API `FieldCacheMixin` with
`field.is_cached(instance)` (see #16043) for working with the model's
fields value cache which I would use for your use case, but I don't think
we should document it.

--
Ticket URL: <https://code.djangoproject.com/ticket/31803#comment:2>

Reply all
Reply to author
Forward
0 new messages