简体   繁体   English

在Django 1.9中将​​会话与mongoengine一起使用

[英]Using sessions with mongoengine in Django 1.9

I need to use a noSQL database on Django, and among the list of unofficial mongo plugins, I decided to go with the legacy mongoengine 0.9 due to its good feedback and relatively comprehensive documentation . 我需要在Django上使用noSQL数据库,并且在非官方的mongo插件列表中,由于其良好的反馈和相对全面的文档 ,我决定使用旧版mongoengine 0.9。

After days of trial and error, I'm able to connect to my mongoDB, create a custom user model (document), save user in DB, and even use Django's auth function like authenticate() , set_password() . 经过数天的反复试验,我能够连接到mongoDB,创建自定义用户模型(文档),将用户保存在DB中,甚至使用Django的auth函数(如authenticate()set_password()

Problem arises when I'm trying to use login(request,user) . 当我尝试使用login(request,user) The line that crashes in that method includes: 在该方法中崩溃的行包括:

  1. request.session[SESSION_KEY] = user._meta.pk.value_to_string(user) , given that the mongo user document doesn't have the attribute pk in meta . 假设mongo用户文档在meta中没有pk属性,则request.session[SESSION_KEY] = user._meta.pk.value_to_string(user) I'm able to get through this by changing it to user.id directly, setting the Mongo's default id as the SESSION_KEY . 我可以通过将其直接更改为user.id并将Mongo的默认ID设置为SESSION_KEY

  2. return get_user_model()._meta.pk.to_python(request.session[SESSION_KEY]) in _get_user_session_key(request) . _get_user_session_key(request) return get_user_model()._meta.pk.to_python(request.session[SESSION_KEY]) _get_user_session_key(request) Which throws a ValidationError that says {{id}} must be an integer . 抛出一个ValidationError ,表示{{id}} must be an integer Dumping get_user_model() on shell returns a mongo_auth.MongoUser and its _meta.pk reads (AutoField id) , which explains why to_python expects an integer value. 在外壳上转储get_user_model()返回mongo_auth.MongoUser及其_meta.pk读取(AutoField id) _meta.pk (AutoField id) ,这说明了为什么to_python需要整数值。

Is there a mongoengine way to use Django's session actually? 实际上,有没有mongoengine方法可以使用Django的会话? I've set mongoengine.django.sessions as Session Engine in my settings.py , but not sure if there's anything else I need to do with it. 我已经在我的settings.py中将mongoengine.django.sessions设置为Session Engine,但是不确定是否还需要做其他事情。

At this point of handling mongoengine's errors, I'm actually feeling quite unconvinced for its stability with Django, is there any other better option I should consider? 在处理mongoengine的错误时,我实际上对它与Django的稳定性感到不太信服,我还有其他更好的选择吗?

mongoengine v0.9 is built for Django 1.5, and the Django support has been separated from the mongoengine repo as of v0.10. mongoengine v0.9是为Django 1.5构建的,并且从mongoengine开始,Django的支持已与mongoengine库分离。

This is where the current repo is for the support of Django 1.9: https://github.com/MongoEngine/django-mongoengine . 这是当前回购用于支持Django 1.9的地方: https : //github.com/MongoEngine/django-mongoengine The plugin still requires testing but so far auth and session works well on my project. 该插件仍然需要测试,但是到目前为止,auth和session在我的项目上都可以正常工作。

Discussion on this topic will be continued on the repo's issue tracker. 关于此主题的讨论将在回购的问题跟踪器上继续进行。 Feel free to join and contribute. 随时加入并做出贡献。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM