Django QuerySet - only() vs defer() vs exclude()
If you have some fields in your Django model that contain a lot of data and you don't need those fields for a particular query, you can tell Django not to retrieve them with
defer():Event.objects.defer("description")While
defer()works at the attribute level,exclude()works on the row level.In other words,
exclude()arguments are used after theWHEREclause -- i.e.,SELECT * FROM users WHERE name = 'jan'-- whiledefer()changes*to the provided fields -- i.e.,SELECT name, email FROM users.Opposite to
defer()isonly(). If you have fewer fields that you want to retrieve than those you don't, you can useonly()to retrieve only the fields provided as arguments:Event.objects.only("title")