#22492: provide a way to prevent database queries on model objects
-------------------------------------+-------------------------------------
Reporter: cjerdonek | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: 1.6
(models, ORM) | Keywords:
Severity: Normal | model,queryset,defer,only
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
This is a feature request to provide a way to prevent database queries
from happening in a block of code, especially on model objects (e.g. using
a context manager, or an internal flag on model objects).
The motivation is for use with the QuerySet method
[https://docs.djangoproject.com/en/dev/ref/models/querysets/#only
`only()`], for example. (The `only()` method is used to prevent
unnecessary fields from being loaded from a database when querying.)
Consider the case of using `only()` to retrieve a list of many model
objects, and then subsequently displaying them. If one executes some
Django code after obtaining this list (e.g. by looping through the list of
objects), it would be bad if this later code accidentally accessed some
other field on each object. This could trigger the unintentional execution
of many individual database queries (e.g. on a production database), with
potentially bad consequences. I don't currently know an easy way to
prevent this.
It would be good to have such a way. For example, Django could provide
some sort of `noQuery()` context manager which would raise an exception if
the database were queried inside it. Code after the `only()` line could
be included in such a context manager. This could prevent accidentally
hammering a database.
Alternatively, the QuerySet API could expose a way to return objects with
some sort of `no_query` flag set. If attribute access on such a model
object required a database query, objects with such a flag set could
instead raise an exception. This would also suffice to prevent accidental
queries.
--
Ticket URL: <https://code.djangoproject.com/ticket/22492>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/052.46d20b5edfa1a9c947292c4e07e9ed83%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.