2010-03-20 2 views
3

MongoEngine을 사용하여 MongoDB를 통합하려고합니다. 표준 pymongo 설정에서 부족한 인증 및 세션 지원을 제공합니다.MongoEngine 사용자 문서를 확장하는 것은 나쁜 습관입니까?

일반 django 인증에서는 모든 곳에서 올바르게 사용될 것이라는 보장이 없기 때문에 User 모델을 확장하는 것은 나쁜 습관으로 간주됩니다. mongoengine.django.auth의 경우입니까?

이 바람직하지 않은 것으로 간주되는 인 경우 별도의 사용자 프로필을 첨부하는 가장 좋은 방법은 무엇입니까? 장고에는 AUTH_PROFILE_MODULE을 지정하는 메커니즘이 있습니다. 이것은 MongoEngine에서도 지원됩니까? 아니면 수동으로 조회를해야합니까?

답변

2

MongoEngine 지금, 당신이 지금 구성 가능한 사용자 개체를 사용할 수 있습니다 장고 1.5 AUTH_PROFILE_MODULE

https://github.com/ruandao/mongoengine_django_contrib_auth/blob/master/models.py#L134-163

+2

답변을 수정하고 이것을 설명하는 링크를 추가 할 수 있습니까? 그것에 관한 정보를 찾을 수없는 것 같습니다. – Soviut

+0

[mongoengine] (https://github.com/MongoEngine/mongoengine/blob/master/mongoengine/django/auth.py#L37-130)의 코드를 확인하고 [django] (https : //github.com/django/django/blob/master/django/contrib/auth/models.py#L379-407) infact는 [this]와 같이 스스로 할 수 있습니다 (https://github.com/ruandao/mongoengine_django_contrib_auth). /blob/master/models.py#L134-163) **주의 : 이것은 캐시를 사용하지 않았습니다 ** – ruandao

4

우리는 단지 확장 사용자 클래스입니다. 그와 내가 말을하는 것이 안전하다고 생각 별도의 객체를 사용하지 않는 것이 큰 이유 그래서

class User(MongoEngineUser): 
    def __eq__(self, other): 
     if type(other) is User: 
      return other.id == self.id 
     return False 

    def __ne__(self, other): 
     return not self.__eq__(other) 

    def create_profile(self, *args, **kwargs): 
     profile = Profile(user=self, *args, **kwargs) 
     return profile 

    def get_profile(self): 
     try: 
      profile = Profile.objects.get(user=self) 
     except DoesNotExist: 
      profile = Profile(user=self) 
      profile.save() 
     return profile 

    def get_str_id(self): 
     return str(self.id) 

    @classmethod 
    def create_user(cls, username, password, email=None): 
     """Create (and save) a new user with the given username, password and 
email address. 
""" 
     now = datetime.datetime.now() 

     # Normalize the address by lowercasing the domain part of the email 
     # address. 
     # Not sure why we'r allowing null email when its not allowed in django 
     if email is not None: 
      try: 
       email_name, domain_part = email.strip().split('@', 1) 
      except ValueError: 
       pass 
      else: 
       email = '@'.join([email_name, domain_part.lower()]) 

     user = User(username=username, email=email, date_joined=now) 
     user.set_password(password) 
     user.save() 
     return user 
0

지원 Django < 1.5에 있지만 어느 시점에서 업그레이드를 원한다면 User 모델을 확장하는 것은 더 이상 나쁜 습관으로 간주되지 않습니다. Django 1.5에서 설정 가능한 사용자 객체는 settings.py에

AUTH_USER_MODEL = 'myapp.MyUser' 

으로 설정됩니다. 이전 사용자 구성에서 변경하는 경우 수집 이름 지정 등에 영향을주는 변경 사항이 있습니다. 1.5로 업그레이드하지 않으려면 지금은 User 객체를 확장 한 다음 나중에 할 일을 업데이트하십시오 1.5로 업그레이드하십시오.

https://docs.djangoproject.com/en/dev/topics/auth/#auth-custom-user

N.B. 개인적으로 Django 1.5 w/MongoEngine에서 이것을 시도하지는 않았지만 지원해야한다고 생각합니다.

+0

'mongoengine.django.auth.User'는 현재 get_profile() 메소드를 가지고 있지 않기 때문에이 방법은 작동하지 않습니다 구현. –

관련 문제