Skip to main content

Command Palette

Search for a command to run...

Django Queryset only and defer

Updated
1 min read
Django Queryset only and defer

In Django, you can use the defer() method on a QuerySet to specify that certain fields should not be retrieved from the database when the QuerySet is evaluated. This can be useful if you have fields in your model that contain large amounts of data and you don't need them for a particular query.

The exclude() method is similar to defer(), but it works at the row level, while defer() works at the attribute level. This means that the arguments passed to exclude() are used in the WHERE clause of the generated SQL query, while defer() changes the list of fields included in the SELECT clause.

The opposite of defer() is only(), which allows you to specify that only certain fields should be retrieved from the database.

More from this blog

P

Python is Love ❤️

42 posts