[Answered ]-The keyword “lazy” with respect to Django

1👍

It means that it isn’t evaluated until you need results from it.

take for example

a = iter([1,2,3])

You know that you have a list of 1,2,3 but you won’t get any values from it until you need to use them in something.

for value in a:
      print(a)

For django specifically, it normally does this when involving the ORM to make queries.

You could write

 n = MyObject.objects.filter(x='foo')

but you might want to chain this query on with other additional filters later on

if x:
     n = n.filter(y="bar")

so to avoid 2 queries happening here, django doesn’t attempt to fetch objects from the database until you start trying to do something with the queryset that would involve using the objects returned from the query

for db_item in n:
     print(db_item)
👤Sayse

Leave a comment