kriwil

aldiantoro nugroho. @kriwil. indonesia. part-time programmer. full-time dreamer. swift. elm. linux. ubuntu. python. django. vim. indiana pacers. manchester united. random.

  1. django's cached_property

    If you find yourself using @property a lot in your model and the property costs database queries, you might want to consider using cached_property instead.

    For example:

    ...
    from django.utils.functional import cached_property
    ...
    
    class Book(models.Model):
        ...
    
        @cached_property
        def references(self):
            ...
            # more queries here
            return references
    

    So instead re-query everytime you call the property, it'll use cache instead, as long as the instance still exists.

    — 20140612 – aldi —
If you have any comment, tweet me @kriwil.