#30874: Allow disabling of lazy loading of model and related fields on querysets
-----------------------------------------+------------------------
               Reporter:  Rowan Seymour  |          Owner:  nobody
                   Type:  Uncategorized  |         Status:  new
              Component:  Uncategorized  |        Version:  2.2
               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              |
-----------------------------------------+------------------------
 Lazy field fetching can be super convenient but also a liability at scale.
 For example:


 {{{
 for book in Book.objects.only("id").all():
   book.author  # lazily fetches author field
   book.categories.all()  # lazily fetches related field
 }}}

 You might be trying to optimize your query by using `only` but then later
 you reference another field and don't realize there's now a new DB hit for
 every object you've loaded.

 What would be really useful in situations like this to disable lazy
 fetching of fields and related fields, so if you haven't included them
 with `only` or `select_related`, the field access raises an exception.

 {{{
 for book in Book.objects(allow_lazy=False).only("id").all():
   book.author  # raises exception
 }}}

 Indicating that you need to correct the query..

 {{{
 for book in Book.objects(allow_lazy=False).only(''id",
 "author").select_related("categories").all():
   book.author
   book.categories.all()
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/30874>
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/055.1e12f08527c53f96b0750df0a670ea36%40djangoproject.com.

Reply via email to