它是不好的做法,延长MongoEngine用户文档?
-
21-09-2019 - |
题
我使用MongoEngine积分MongoDB的。它提供了AUTH和会话支持,一个标准的pymongo设置将缺乏。
在常规Django的权威性,它被认为是不好的做法,扩展用户模型,因为谁也不能保证它会被正确地使用随处可见。这是与mongoengine.django.auth
的情况?
如果它的是的认为是不好的做法,什么是附加一个单独的用户配置文件的最好方法? Django的有指定AUTH_PROFILE_MODULE
机制。这是支持MongoEngine为好,或我应该手动操作的方式查找
解决方案
MongoEngine现在支持AUTH_PROFILE_MODULE
https://github.com/ruandao/mongoengine_django_contrib_auth/斑点/主/ 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,但希望在某个时候进行升级。在Django 1.5时,可配置的用户对象被设定为:
AUTH_USER_MODEL = 'myapp.MyUser'
在您的settings.py。如果你是从以前的用户配置发生变化,有变化是影响集合命名等,如果你不希望升级到1.5,只是还没有,你可以扩展用户对象现在,然后进一步更新后,当你这样做升级到1.5。
https://docs.djangoproject.com/en的/ dev /主题/ AUTH /#AUTH-定制用户
N.B。我没有亲自试过这个在Django 1.5W / MongoEngine,但预计它应该支持它。