3

MongoEngineを使用してMongoDBを統合しています。これは、標準のpymongoセットアップにはない認証とセッションのサポートを提供します。

通常のdjango認証では、どこでも正しく使用される保証がないため、ユーザーモデルを拡張することは悪い習慣と見なされます。これは当てはまり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

注意:MongoEngineを使用したDjango 1.5でこれを個人的に試したことはありませんが、サポートされるはずです。

于 2012-11-14T19:18:21.640 に答える