Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django objects...values() select only some fields

I'm optimizing the memory load (~2GB, offline accounting and analysis routine) of this line:

l2 = Photograph.objects.filter(**(movie.get_selectors())).values()

Is there a way to convince django to skip certain columns when fetching values()?

Specifically, the routine obtains all rows of the table matching certain criteria (db is optimized and performs it very quickly), but it is a bit too much for python to handle - there is a long string referenced in each row, storing the urls for thumbnails.

I only really need three fields from each row, but, if all the fields are included, it suddenly consumes about 5kB/row which sadly pushes the RAM to the limit.

like image 786
qdot Avatar asked Nov 28 '22 18:11

qdot


2 Answers

Check out the QuerySet method, only. When you declare that you only want certain fields to be loaded immediately, the QuerySet manager will not pull in the other fields in your object, till you try to access them.

If you have to deal with ForeignKeys, that must also be pre-fetched, then also check out select_related

The two links above to the Django documentation have good examples, that should clarify their use.


Take a look at Django Debug Toolbar it comes with a debugsqlshell management command that allows you to see the SQL queries being generated, along with the time taken, as you play around with your models on a django/python shell.

like image 44
Roshan Mathews Avatar answered Dec 01 '22 09:12

Roshan Mathews


The values(*fields) function allows you to specify which fields you want.

like image 50
Nicolae Dascalu Avatar answered Dec 01 '22 09:12

Nicolae Dascalu