Lazy Loading of Related Objects
Learn how BlazingAPI lazy loads related objects.
In BlazingAPI, lazy loading allows related objects to be loaded only when they are accessed, rather than when the initial query is made. This approach helps improve the performance of your application by reducing the number of database queries.
Quick example
from blazingapi.orm.models import Model
from blazingapi.orm.fields import VarCharField, TextField, ForeignKeyField
class Book(Model):
title = VarCharField(max_length=255)
class Article(Model):
title = VarCharField(max_length=255)
content = TextField()
book = ForeignKeyField(Book)
# Build the query, the query is not executed yet
articles = Article.manager.all()
# Now the query is executed, when we try to evaluate the result
article = articles[0]
print(article.content) # The related book is not loaded yet
print(article.book.title) # The query for the book is executed now and saved on cache
# The book is accessed from cache, no additional query is made
print(article.book.title)
print(article.book.title)
print(article.book.title)
print(article.book.title)
print(article.book.title)