3

我正在使用 MongoEngine 集成 MongoDB。它提供了标准 pymongo 设置所缺乏的身份验证和会话支持。

在常规的 django 身份验证中,扩展 User 模型被认为是不好的做法,因为不能保证它会在任何地方正确使用。是这种情况mongoengine.django.auth吗?

如果认为这不好的做法,那么附加单独的用户配置文件的最佳方法是什么?Django 具有指定AUTH_PROFILE_MODULE. MongoEngine 是否也支持此功能,还是我应该手动进行查找?

4

3 回答 3

4

我们只是扩展了 User 类。

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
于 2010-05-26T03:38:30.187 回答
2

MongoEngine 现在支持AUTH_PROFILE_MODULE

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

于 2012-08-22T22:07:36.930 回答
0

在 Django 1.5 中,您现在可以使用可配置的用户对象,因此这是不使用单独对象的一个​​很好的理由,我认为可以肯定地说,如果您使用 Django <1.5,扩展用户模型不再被认为是不好的做法但希望在某个时候升级。在 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 中亲自尝试过,但希望它应该支持它。

于 2012-11-14T19:18:21.640 回答