Shai, `execute_wrapper` can be useful to prevent any queries from happening at all but I believe there's merit in wanting to prevent objects retrieved from a queryset from executing ghost queries during manipulations of the returned `Model` instances while allowing other unrelated queries to be performed.
For example, what if a database based cache backend needs to be called during a queryset serialization, or that the `ContentType` in-memory cache is not warmed up during serialization, or that any other kind of query, that is not linked to the manipulated queryset needs to be executed during the serialization process. Adam, While I understand the desire to make life easier for Django newcomers through automated ORM optimization I feel like there should still be a way to help developers familar with existing manual optimization techniques to avoid unexpected queries when they've clearly defined what they expect the ORM to do. I feel like a few contributors in the thread you've linked to express similar concerns. e.g. > I completely agree that visibility of this problem is a major > issue, and would really welcome work on improving this, > especially in DEBUG mode. One option might be a method that > replaces lazy loads with exceptions. - Luke Plant[0] Best, Simon [0] https://groups.google.com/d/msg/django-developers/EplZGj-ejvg/cHyFdoaXCAAJ Le jeudi 4 janvier 2018 05:55:21 UTC-5, Adam Johnson a écrit : > > 1. I don't think the seal API on the QuerySet is enough, there are other > ways implicit queries can be triggered without a QuerySet, e.g. > Book(author_id=1).author . Shai's suggestion of using a query execution > blocker is probably the best approach. If you were really worried you could > even implement it the other way round, have a permanent block and then > 'unlock it' around blocks of code you expect to make queries. > > 2. https://github.com/YPlan/django-perf-rec is assertNumQueries on > steroids and we used it at YPlan to great success :) > > 3. auto-prefetch would get rid of a lot of N+1 query problems: > https://groups.google.com/forum/#!topic/django-developers/EplZGj-ejvg > > On 4 January 2018 at 06:28, Shai Berger <sh...@platonix.com <javascript:>> > wrote: > >> Hi all, >> >> Django 2.0 has a new feature[1] which allows you to say "I expect no >> actual >> database queries in this piece of code". It is not designed to stop a >> specific >> queryset from spawning requests, so getting it to do exactly what's asked >> for >> in this thread may be a little involved, but if your goal is to prevent >> surprise queries, I think it is actually better than sealing a single >> queryset. >> >> HTH, >> Shai >> >> [1] https://docs.djangoproject.com/en/2.0/topics/db/instrumentation/ >> > > > > -- > Adam > -- You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-developers+unsubscr...@googlegroups.com. To post to this group, send email to django-developers@googlegroups.com. Visit this group at https://groups.google.com/group/django-developers. To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/44eac846-97ee-481a-8136-4388eaf62a39%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.