Django redis connection interface or how to implement it

Is there any plugin or third-party backend for managing redis connections in Django, so the methods in view.py should not explicitly connect to redis for each request?

If not, how would you start implementing it? New plugin? new backend? new django middleware?

Thank.

+5
source share
1 answer

I think the new standard for non-rel django-nonrel databases . I don't know if django-nonrel is ready or redis support is ready, but they have a guide on writing a custom backend without sql .

, , redis django , DatabaseBackend. django , ACID. syncdb? Querysets?

, models.Manager . :

# helper   
def fill_model_instance(instance, values):
    """ Fills an model instance with the values from dict values """                                    
    attributes = filter(lambda x: not x.startswith('_'), instance.__dict__.keys())

    for a in attributes:
        try:
            setattr(instance, a, values[a.upper()])
            del values[a.upper()]
        except:
            pass

    for v in values.keys():
        setattr(instance, v, values[v])

    return instance




class AuthorManager( models.Manager ):

    # You may try to use the default methods.
    # But should be freaking hard...
    def get_query_set(self):
        raise NotImplementedError("Maybe you can write a Non relational Queryset()! ")

    def latest(self, *args, **kwargs):
        # redis Latest query
        pass

    def filter(self, *args, **kwargs):
       # redis filter query
       pass

    # Custom methods that you may use, instead of rewriting
    # the defaults ones.
    def open_connection(self):
        # Open a redis connection
        pass

    def search_author( self, *args, **kwargs ):
        self.open_connection()

        # Write your query. I don't know how this shiny non-sql works.
        # Assumes it returns a dict for every matched author.
        authors_list = [{'name': 'Leibniz',   'email': 'iinventedcalculus@gmail.com'},
                         'name': 'Kurt Godel','email': 'self.consistent.error@gmail.com'}]

        return [fill_instance(Author(), author) for author in authors_list]



class Author( models.Model ):
    name      = models.CharField( max_length = 255 )
    email     = models.EmailField( max_length = 255 )

     def save(self):
         raise NotImplementedError("TODO: write a redis save")

     def delete(self):
         raise NotImplementedError(""TODO: write a delete save")

     class Meta:
          managed = False

, , , django. . -.

+5

All Articles