views:

109

answers:

1

One of my django models has a large TextField which I often don't need to use. Is there a way to tell django to "lazy-load" this field? i.e. not to bother pulling it from the database unless I explicitly ask for it. I'm wasting a lot of memory and bandwidth pulling this TextField into python every time I refer to these objects.

The alternative would be to create a new table for the contents of this field, but I'd rather avoid that complexity if I can.

+3  A: 

The functionality happens when you make the query, using the defer() statement, instead of in the model definition. Check it out here in the docs: http://docs.djangoproject.com/en/dev/ref/models/querysets/#defer-fields

Now, actually, your alternative solution of refactoring and pulling the data into another table is a really good solution. Some people would say that the need to lazy load fields means there is a design flaw, and the data should have been modeled differently.

Either way works, though!

Alan Christopher Thomas