编程语言
首页 > 编程语言> > python – Django – 与mongoengine DB的Auth

python – Django – 与mongoengine DB的Auth

作者:互联网

我想用我的mongoengine db处理我的Django项目中的身份验证.

我尝试了一些关于这些问题的例子,这些问题在旧问题中得到了回答,我正在使用Django 1.6和mongoengine.一切都已安装,运行,我可以创建文档并将其保存到Mongoengine DB.

我跟着http://mongoengine-odm.readthedocs.org/en/latest/django.html

我收到以下错误:

当我跑:

>>> from django.contrib.auth.models import User
>>> user = User.objects.create_user('john', 'lennon@thebeatles.com', 'johnpassword')

我明白了:

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'
>>> 

我真的不明白两件事:

– 我是否必须创建和定义用户将被存储的数据库,或者它们将自动创建?

– 什么是经理?我没有定义任何经理的东西

在开始时我认为寄存器保存在数据库中.称为’mongo_auth.MongoUser’,但它并没有将它保存在任何地方.

这是模型:

# 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已正确配置.

编辑@cestDiego:

我的设置完全相同,我注意到Db后端,因为它创建了一个我不感兴趣的数据库因为我使用mongo …无论如何我正在使用mongoengine.django.auth导入用户现在但是当我尝试创建一个用户,它返回给我:

>>> user = User.objects.create_user('john', 'lennon@thebeatles.com', 'johnpassword')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'QuerySet' object has no attribute 'create_user'

也许我们正在定制auth,这就是为什么不工作,不知道.你也有这个问题吗?

第二次编辑:

我正在阅读,我们必须使用Djangos auth,配置正确的设置后,我们都做了.

然后必须导入来自django.contrib.auth导入身份验证并使用在Django docs中提供的authenticate,希望有所帮助; D.

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.")

解决方法:

嘿,我和你一样.我可以弄清楚你在settings.py中有这些

AUTH_USER_MODEL = 'mongo_auth.MongoUser'
MONGOENGINE_USER_DOCUMENT = 'mongoengine.django.auth.User'

这在已安装的应用程序中

'mongoengine.django.mongo_auth'

这意味着现在您正在使用Mongoengine身份验证方法,您使用的第一行导入了DJANGO身份验证方法,因此存在问题.您不是在mongodb中创建任何数据库,而是在Django的ORM中使用backend.dummy设置的虚拟数据库.

我不知道如何使用mongoengine的auth方法,如果你搞清楚,请向我解释一下;)我希望我澄清一下我们在这里面临的问题.这只是一个更深刻阅读文档的问题.

编辑:(答案后1分钟)
我在你链接到的文档中找到了这个:

MongoEngine includes a Django authentication backend, which uses MongoDB. >The User model is a MongoEngine Document, but implements most of the >methods and attributes that the standard Django User model does – so the >two are moderately compatible.

所以这意味着在你的情况下,交换

from django.contrib.auth import User

from mongoengine.django.auth import User

标签:mongoengine,python,authentication,django,django-models
来源: https://codeday.me/bug/20190928/1827521.html