102π
In Django 1.1 onwards, you can use defer('col1', 'col2')
to exclude columns from the query, or only('col1', 'col2')
to only get a specific set of columns. See the documentation.
values
does something slightly different β it only gets the columns you specify, but it returns a list of dictionaries rather than a set of model instances.
- [Django]-How can I temporarily disable a foreign key constraint in MySQL?
- [Django]-Setting the selected value on a Django forms.ChoiceField
- [Django]-How to configure where to redirect after a log out in Django?
2π
The accepted answer advises defer
and only
, however the docs discourage this in most cases.
only use defer() when you cannot, at queryset load time, determine if you will need the extra fields or not. If you are frequently loading and using a particular subset of your data, the best choice you can make is to normalize your models and put the non-loaded data into a separate model (and database table). If the columns must stay in the one table for some reason, create a model with Meta.managed = False (see the managed attribute documentation) containing just the fields you normally need to load and use that where you might otherwise call defer(). This makes your code more explicit to the reader, is slightly faster and consumes a little less memory in the Python process.
- [Django]-Django: reverse accessors for foreign keys clashing
- [Django]-Allowing RabbitMQ-Server Connections
- [Django]-Django Server Error: port is already in use