def __getitem__(self, key):
        """
        Returns the setting value for ``key`` from the cache if possible,
        otherwise from the database. Adds values that are fetched from the
        database to the cache.
        """
        # Populate the cache on first access.
        if self.empty_cache:
            self.refresh()
        # Get the value from the cache.
        try:
            value = super(SettingDict, self).__getitem__(key)
        except KeyError:
            # Get the value from the database.
            try:
                value = self.model.objects.get(name=key).value
            except self.model.DoesNotExist:
                if self.default is None:
                    raise KeyError(key)
                # Create setting with default value.
                value = self.default
                self.model.objects.create(name=key, value=value)
            # Update the cache.
            super(SettingDict, self).__setitem__(key, value)
        return value
def __getitem__ ( self , key ) : if self . empty_cache : self . refresh ( ) try : value = super ( SettingDict , self ) . __getitem__ ( key ) except KeyError : try : value = self . model . objects . get ( name = key ) . value except self . model . DoesNotExist : if self . default is None : raise KeyError ( key ) value = self . default self . model . objects . create ( name = key , value = value ) super ( SettingDict , self ) . __setitem__ ( key , value ) return value
Returns the setting value for ``key`` from the cache if possible,
        otherwise from the database. Adds values that are fetched from the
        database to the cache.