MongoEngineユーザー文書を拡張するための悪い習慣ですか?
-
21-09-2019 - |
質問
私はMongoEngineを使用してMongoDBのを統合しています。これは、標準pymongoの設定が欠けてしまうことを認証およびセッションのサポートを提供しています。
は、通常のDjangoの認証では、正確にどこにでも使用される保証はありませんので、悪い習慣は、Userモデルを拡張するために考えられています。これはmongoengine.django.auth
の場合ですか?
それはがの場合には悪い習慣と考えられ、別のユーザープロファイルを添付するための最良の方法は何ですか? DjangoはAUTH_PROFILE_MODULE
を指定するためのメカニズムを持っています。これは、同様MongoEngineでサポートされている、または私は手動でルックアップを実行すべきか?
解決
MongoEngineサポートするようになりましAUTH_PROFILE_MODULE
https://github.com/ruandao/mongoengine_django_contrib_auth/ブロブ/マスター/ models.py#L134-163する
他のヒント
私たちだけの拡張の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
でDjangoの1.5あなたは今ではそれが別のオブジェクトを使用して、私はそれはもはやあなたがオンになっている場合、ユーザーモデルを拡張するために悪い習慣と考えられていることを言わないことは安全だとは思わないに大きな理由ですが、設定可能なユーザーオブジェクトを使用することができますジャンゴ<1.5が、どこかの時点でアップグレードすることを期待。ジャンゴ1.5において、設定ユーザオブジェクトがで設定されている
AUTH_USER_MODEL = 'myapp.MyUser'
あなたのsettings.pyで。あなたが以前のユーザー設定から変更している場合、あなたはまだ1.5にアップグレードしたくない場合は、衝撃のコレクションの命名など、あなたが今のユーザーオブジェクトを拡張し、その後、さらに後にあなたがやるときにそれを更新することができ、変更がありました1.5にアップグレードします。
https://docs.djangoproject.com/en / dev /トピック/認証/#のauth-カスタムユーザーの
N.B。私は個人的に/ MongoEngineワットジャンゴ1.5でこれを試してみましたが、それはそれをサポートする必要があります期待していなかった。