2010-12-28 3 views
5

Django에서 redis 연결을 관리하기위한 플러그인 또는 타사 백엔드가 있으므로 view.py의 메소드가 모든 요청에 ​​대해 redis에 명시 적으로 연결할 필요가 없습니까?Django redis connection backend 또는 구현 방법

그렇지 않은 경우 어떻게 구현 하시겠습니까? 새로운 플러그인? 새로운 백엔드? 새로운 장고 미들웨어?

감사합니다.

답변

5

비 rel 데이터베이스의 새로운 표준은 django-nonrel입니다. django-nonrel이 생산 준비가되었거나 지원이 redis인지 여부는 알 수 없지만 writing a custom no-sql backend에 대한 안내가 있습니다.

불행히도, 표준 django에 redis에 대한 지원을 작성하는 것이 DatabaseBackend으로 작성하는 것이 쉽지 않다고 생각합니다. 장고 모델에는 단순히 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': '[email protected]'}, 
         'name': 'Kurt Godel','email': '[email protected]'}] 

     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 

. 나는이 코드를 테스트하고 실행 한 을 가지고 있지 않다. 나는 먼저 장고미르를 조사 할 것을 제안한다.

+0

답장을 보내 주셔서 감사합니다. 내 모델을 redis 인스턴스에 저장할 필요가 없으므로 장고 - 비 (django-nonrel)가 필요하다고 생각하지 않습니다. 내가 직접 연결 관리 모듈을 구현할 것이다. – simao