'Retrieves and caches the value from the datastore on the first lookup.
Returns the cached value.'

def __get__(self, instance, owner):

    from django.db.models.fields import FieldDoesNotExist
    assert (instance is not None)
    cls = self.model_ref()
    data = instance.__dict__
    if (data.get(self.field_name, self) is self):
        try:
            cls._meta.get_field_by_name(self.field_name)
            name = self.field_name
        except FieldDoesNotExist:
            name = [f.name for f in cls._meta.fields if (f.attname == self.field_name)][0]
        val = getattr(cls._base_manager.filter(pk=instance.pk).only(name).using(instance._state.db).get(), self.field_name)
        data[self.field_name] = val
    return data[self.field_name]
