hierarkey/models.py
def add(self, cache_namespace: str = None, parent_field: str = None) -> type:
        """
        Decorator. Attaches a global key-value store to a Django model.

        :param cache_namespace: Optional. A custom namespace used for caching. By default this is
                                constructed from the name of the class this is applied to and
                                the ``attribute_name`` of this ``Hierarkey`` object.
        :param parent_field: Optional. The name of a field of this model that refers to the parent
                             in the hierarchy. This must be a ``ForeignKey`` field.
        """
        if isinstance(cache_namespace, type):
            raise ImproperlyConfigured('Incorrect decorator usage, you need to use .add() instead of .add')

        def wrapper(model):
            if not issubclass(model, models.Model):
                raise ImproperlyConfigured('Hierarkey.add() can only be invoked on a Django model')

            _cache_namespace = cache_namespace or ('%s_%s' % (model.__name__, self.attribute_name))

            attrs = self._create_attrs(model)
            attrs['object'] = models.ForeignKey(model, related_name='_%s_objects' % self.attribute_name,
                                                on_delete=models.CASCADE)
            model_name = '%s_%sStore' % (model.__name__, self.attribute_name.title())
            kv_model = self._create_model(model_name, attrs)

            setattr(sys.modules[model.__module__], model_name, kv_model)

            hierarkey = self

            def prop(iself):
                from .proxy import HierarkeyProxy

                attrname = '_hierarkey_proxy_{}_{}'.format(_cache_namespace, self.attribute_name)
                cached = getattr(iself, attrname, None)
                if not cached:
                    try:
                        parent = getattr(iself, parent_field) if parent_field else None
                    except models.ObjectDoesNotExist:  # pragma: no cover
                        parent = None

                    if not parent and hierarkey.global_class:
                        parent = hierarkey.global_class()

                    cached = HierarkeyProxy._new(
                        iself,
                        type=kv_model,
                        hierarkey=hierarkey,
                        parent=parent,
                        cache_namespace=_cache_namespace
                    )
                    setattr(iself, attrname, cached)
                return cached

            setattr(model, self.attribute_name, property(prop))

            return model

        return wrapper

        Decorator. Attaches a global key-value store to a Django model.

        :param cache_namespace: Optional. A custom namespace used for caching. By default this is
                                constructed from the name of the class this is applied to and
                                the ``attribute_name`` of this ``Hierarkey`` object.
        :param parent_field: Optional. The name of a field of this model that refers to the parent
                             in the hierarchy. This must be a ``ForeignKey`` field.
        
Decorator. Attaches a global key-value store to a Django model.
Decorator . Attaches a global key - value store to a Django model .
def add(self, cache_namespace: str = None, parent_field: str = None) -> type:
        if isinstance(cache_namespace, type):
            raise ImproperlyConfigured('Incorrect decorator usage, you need to use .add() instead of .add')
        def wrapper(model):
            if not issubclass(model, models.Model):
                raise ImproperlyConfigured('Hierarkey.add() can only be invoked on a Django model')
            _cache_namespace = cache_namespace or ('%s_%s' % (model.__name__, self.attribute_name))
            attrs = self._create_attrs(model)
            attrs['object'] = models.ForeignKey(model, related_name='_%s_objects' % self.attribute_name,
                                                on_delete=models.CASCADE)
            model_name = '%s_%sStore' % (model.__name__, self.attribute_name.title())
            kv_model = self._create_model(model_name, attrs)
            setattr(sys.modules[model.__module__], model_name, kv_model)
            hierarkey = self
            def prop(iself):
                from .proxy import HierarkeyProxy
                attrname = '_hierarkey_proxy_{}_{}'.format(_cache_namespace, self.attribute_name)
                cached = getattr(iself, attrname, None)
                if not cached:
                    try:
                        parent = getattr(iself, parent_field) if parent_field else None
                    except models.ObjectDoesNotExist:  
                        parent = None
                    if not parent and hierarkey.global_class:
                        parent = hierarkey.global_class()
                    cached = HierarkeyProxy._new(
                        iself,
                        type=kv_model,
                        hierarkey=hierarkey,
                        parent=parent,
                        cache_namespace=_cache_namespace
                    )
                    setattr(iself, attrname, cached)
                return cached
            setattr(model, self.attribute_name, property(prop))
            return model
        return wrapper
def add ( self , cache_namespace : str = None , parent_field : str = None ) -> type : if isinstance ( cache_namespace , type ) : raise ImproperlyConfigured ( 'Incorrect decorator usage, you need to use .add() instead of .add' ) def wrapper ( model ) : if not issubclass ( model , models . Model ) : raise ImproperlyConfigured ( 'Hierarkey.add() can only be invoked on a Django model' ) _cache_namespace = cache_namespace or ( '%s_%s' % ( model . __name__ , self . attribute_name ) ) attrs = self . _create_attrs ( model ) attrs [ 'object' ] = models . ForeignKey ( model , related_name = '_%s_objects' % self . attribute_name , on_delete = models . CASCADE ) model_name = '%s_%sStore' % ( model . __name__ , self . attribute_name . title ( ) ) kv_model = self . _create_model ( model_name , attrs ) setattr ( sys . modules [ model . __module__ ] , model_name , kv_model ) hierarkey = self def prop ( iself ) : from . proxy import HierarkeyProxy attrname = '_hierarkey_proxy_{}_{}' . format ( _cache_namespace , self . attribute_name ) cached = getattr ( iself , attrname , None ) if not cached : try : parent = getattr ( iself , parent_field ) if parent_field else None except models . ObjectDoesNotExist : parent = None if not parent and hierarkey . global_class : parent = hierarkey . global_class ( ) cached = HierarkeyProxy . _new ( iself , type = kv_model , hierarkey = hierarkey , parent = parent , cache_namespace = _cache_namespace ) setattr ( iself , attrname , cached ) return cached setattr ( model , self . attribute_name , property ( prop ) ) return model return wrapper
Decorator.
Decorator .
"""
        Decorator. Attaches a global key-value store to a Django model.

        :param cache_namespace: Optional. A custom namespace used for caching. By default this is
                                constructed from the name of the class this is applied to and
                                the ``attribute_name`` of this ``Hierarkey`` object.
        :param parent_field: Optional. The name of a field of this model that refers to the parent
                             in the hierarchy. This must be a ``ForeignKey`` field.
        """ # pragma: no cover
self
cache_namespace
str
Optional. A custom namespace used for caching. By default this is
constructed from the name of the class this is applied to and
the ``attribute_name`` of this ``Hierarkey`` object.
Optional . A custom namespace used for caching . By default this is constructed from the name of the class this is applied to and the ` ` attribute_name ` ` of this ` ` Hierarkey ` ` object .
parent_field
str
Optional. The name of a field of this model that refers to the parent
in the hierarchy. This must be a ``ForeignKey`` field.
Optional . The name of a field of this model that refers to the parent in the hierarchy . This must be a ` ` ForeignKey ` ` field .