Chcę obsłużyć uwierzytelnienia w moim projekcie Django z moją bazą danych Mongoengine.Django - Auth z mongoengine DB
Próbowałem kilku przykładów na ten temat odpowiedziałem w starych pytaniach, ale nie działał. Używam Django 1.6 i mongoengine. Wszystko jest zainstalowane, działa i mogę tworzyć i zapisywać dokumenty w mojej bazie danych Mongoengine.
Obserwuję http://mongoengine-odm.readthedocs.org/en/latest/django.html
i pojawia się następujący błąd:
Kiedy biegnę:
>>> from django.contrib.auth.models import User
>>> user = User.objects.create_user('john', '[email protected]', 'johnpassword')
uzyskać to:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/REBORN/reb_env/local/lib/python2.7/site-packages/django/db/models/manager.py", line 273, in __get__
self.model._meta.object_name, self.model._meta.swapped
AttributeError: Manager isn't available; User has been swapped for 'mongo_auth.MongoUser'
>>>
naprawdę don” t zrozumieć 2 rzeczy:
-Do muszę utworzyć i zdefiniować bazę danych, w której będą zapisywane użytkownicy, lub będą one tworzone automatycznie?
-Czym jest menedżer? Nie zdefiniowałem żadnego menedżera rzeczy
Na początku myślałem, że rejestr został zapisany w db. zwany "mongo_auth.MongoUser", ale nie zapisał go w żadnym miejscu.
Oto modele:
# Create your models here.
from mongoengine import *
class Profile(Document):
email = StringField(required=True)
first_name = StringField(max_length=50)
last_name = StringField(max_length=50)
class auth_user(Document):
username = StringField(max_length=50)
email = StringField(max_length=50)
password = StringField(max_length=50)
settings.py jest prawidłowo skonfigurowany jak mówi instrukcja.
EDIT @cestDiego:
Moje ustawienia są dokładnie takie same, zauważyłem o backend Db ponieważ tworzy bazę danych, która mnie nie interesuje, bo używam Mongo ... W każdym razie jestem z ussing mongoengine.django.auth import użytkownika, ale teraz gdy próbuję utworzyć Użytkownikowi zwraca mi:
>>> user = User.objects.create_user('john', '[email protected]', 'johnpassword')
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'QuerySet' object has no attribute 'create_user'
Może jesteśmy dostosowywania uwierzytelniania i dlatego nie działa, nie mam pojęcia. Czy masz również ten problem?
DRUGA EDYCJA:
czytałem i musimy użyć Djangos auth po skonfigurować odpowiednie ustawienia, a zarówno zrobiliśmy.
Następnie należy zaimportować uwierzytelnienie importu z django.contrib.auth i użyć uwierzytelniania, jak jest to przewidziane w dokumentach Django, z nadzieją, że pomoże;
from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse
from game.models import *
from mongoengine import *
from models import User
from django.contrib.auth import authenticate
def login(request):
user = authenticate(username='john', password='secret')
if user is not None:
# the password verified for the user
if user.is_active:
print("User is valid, active and authenticated")
else:
print("The password is valid, but the account has been disabled!")
else:
# the authentication system was unable to verify the username and password
print("The username and password were incorrect.")
Otrzymuję błąd w tej linii user.backend = 'mongoengine.django.auth.MongoEngineBackend' Błąd jest "MetaDict" obiekt ma brak atrybutu "pk" –