Domanda

sto integrando MongoDB usando MongoEngine. Esso fornisce autenticazione e la sessione di supporto che una configurazione standard di pymongo mancherebbe.

Nel regolare auth Django, è considerato cattiva pratica di estendere il modello per l'utente in quanto non v'è alcuna garanzia che verrà utilizzato in modo corretto ovunque. È questo il caso di mongoengine.django.auth?

Se è considerato cattiva pratica, qual è il modo migliore per collegare un profilo utente separata? Django ha meccanismi per specificare un AUTH_PROFILE_MODULE. È questo supportato in MongoEngine pure, o dovrei fare manualmente la ricerca?

È stato utile?

Altri suggerimenti

Abbiamo appena esteso la classe utente.

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

In Django 1.5 è ora possibile utilizzare un oggetto configurabile dall'utente, quindi questo è un buon motivo per non utilizzare un oggetto separato e penso che sia giusto dire che non è più considerato una cattiva pratica di estendere il modello utente, se siete su Django <1.5, ma in attesa di aggiornare ad un certo punto. In Django 1.5, l'oggetto utente configurabile è impostato con:

AUTH_USER_MODEL = 'myapp.MyUser'

nel vostro settings.py. Se si desidera passare da una configurazione utente precedente, ci sono cambiamenti che la raccolta impatto denominazione ecc Se non si desidera eseguire l'aggiornamento a 1.5 appena ancora, è possibile estendere l'oggetto d'uso, per ora, e quindi aggiornare ulteriormente più tardi, quando si fa l'aggiornamento a 1.5.

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

NB. Non ho provato personalmente questo Django 1.5 w / MongoEngine, ma si aspettano che dovrebbe sostenerlo.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top