扩展 MongoEngine 用户文档是不好的做法吗?

发布于 2024-08-25 11:14:21 字数 293 浏览 7 评论 0原文

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

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

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

I'm integrating MongoDB using MongoEngine. It provides auth and session support that a standard pymongo setup would lack.

In regular django auth, it's considered bad practice to extend the User model since there's no guarantee it will be used correctly everywhere. Is this the case with mongoengine.django.auth?

If it is considered bad practice, what is the best way to attach a separate user profile? Django has mechanisms for specifying an AUTH_PROFILE_MODULE. Is this supported in MongoEngine as well, or should I be manually doing the lookup?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

因为看清所以看轻 2024-09-01 11:14:21

我们刚刚扩展 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

We just extended the User class.

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
z祗昰~ 2024-09-01 11:14:21

在 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/ topic/auth/#auth-custom-user

注意,我还没有亲自在 Django 1.5 w/ MongoEngine 中尝试过这个,但希望它应该支持它。

In Django 1.5 you can now use a configurable user object, so that's a great reason to not use a separate object and I think it's safe to say that it is no longer considered bad practice to extend the User model if you are on Django <1.5 but expecting to upgrade at some point. In Django 1.5, the configurable user object is set with:

AUTH_USER_MODEL = 'myapp.MyUser'

in your settings.py. If you are changing from a previous user configuration, there are changes that impact collection naming etc. If you don't want to upgrade to 1.5 just yet, you can extend the User object for now, and then update it further later when you do upgrade to 1.5.

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

N.B. I have not personally tried this in Django 1.5 w/ MongoEngine, but expect it should support it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文