Lazy Load In Loop (ARCH2059)
Esta página aún no está disponible en tu idioma.
Key python.orm.lazy-load-in-loop · Severity warning · Category orm
Accessing a lazily-loaded relation on each iterated element is the runtime trigger of N+1 when eager loading was omitted. Load the relation up front.
Incorrect
Section titled “Incorrect”for book in Book.objects.all(): print(book.author.name) # a lazy SELECT per bookCorrect
Section titled “Correct”for book in Book.objects.select_related("author"): print(book.author.name) # author loaded in one JOINEager-load the relation accessed in the loop with select_related / prefetch_related.