扩展MongoEngine用户文档是不好的做法吗?
我正在使用MongoEngine集成MongoDB。它提供了一个标准的pymongo设置缺乏的认证和会话支持。扩展MongoEngine用户文档是不好的做法吗?
在普通的django auth中,扩展User模型被认为是不好的做法,因为不能保证它在任何地方都能正确使用。这是mongoengine.django.auth
的情况吗?
如果它是被认为是不好的做法,什么是最好的方式来附加一个单独的用户配置文件? Django有指定AUTH_PROFILE_MODULE
的机制。这是否也支持MongoEngine,或者我应该手动进行查找?
MongoEngine现在支持AUTH_PROFILE_MODULE
https://github.com/ruandao/mongoengine_django_contrib_auth/blob/master/models.py#L134-163
我们只是扩展的用户类。
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
在Django的1.5现在可以使用一个可配置的用户对象,所以这是一个伟大的理由不使用一个单独的对象,我认为它是安全的说如果你使用的是Django < 1.5但是期望在某个时候升级,那么扩展User模型已不再被认为是不好的做法。在Django 1.5,可配置的用户对象被设定为:
AUTH_USER_MODEL = 'myapp.MyUser'
在你的settings.py
。如果您正在更改以前的用户配置,则会有一些更改会影响集合名称等。如果您尚不想升级到1.5,则可以暂时扩展User对象,然后在您稍后进一步更新它时升级到1.5。
https://docs.djangoproject.com/en/dev/topics/auth/#auth-custom-user
注:我没有在Django 1.5 w/MongoEngine中亲自尝试过,但期望它应该支持它。
没有这不起作用,因为'mongoengine.django.auth.User'目前没有get_profile()方法实现。 – 2012-11-22 13:40:34
你能编辑你的答案并添加一个解释这个链接的链接吗?我似乎无法找到任何有关它的信息。 – Soviut 2012-08-23 18:21:40
只需检查[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)事实上,你可以自己做这个[this](https://github.com/ruandao/mongoengine_django_contrib_auth /blob/master/models.py#L134-163)**注意:这不是使用缓存** – ruandao 2012-08-24 00:33:02