简体   繁体   English

使用MongoEngine用户进行Django Rest Framework令牌认证

[英]Django Rest Framework Token Authentication with MongoEngine User

I am using a Django installation with MongoEngine to get MongoDB to work as my project's backend. 我正在将MongoEngine与Django安装在一起使用,以使MongoDB可以用作项目的后端。

I've got the implementation to work but am stuck with Django Rest Framework's authentication system. 我已经实现了该工作,但仍受Django Rest Framework的身份验证系统的困扰。 Was just reading the API guide for their authentication chapter and was trying out their TokenAuthentication . 刚刚阅读了有关authentication一章的API指南,并尝试了他们的TokenAuthentication

Tokens are created using 令牌是使用以下方式创建的

t = Token.objects.create(user=..)

and it expects a Django User instance. 并且它需要一个Django User实例。 Since I am using MongoEngine, my database entry in the settings.py file is set to Dummy. 由于我使用的是MongoEngine,因此我在settings.py文件中的数据库条目设置为Dummy。

So how do I create a user instance that can be used by Token class. 因此,如何创建可由Token类使用的user实例。

I tried creating users using MongoEngine's mongoengine.django.auth but the Token class isn't accepting this object. 我尝试使用MongoEngine的mongoengine.django.auth创建用户,但Token类不接受该对象。

The resulting error is: 产生的错误是:

ValueError: Cannot assign "<User: gaurav>": "Token.user" must be a "User" instance.

Please let me know how I can get this to work. 请让我知道如何使它工作。

Unfortunately you have to write it for yourself. 不幸的是,您必须自己编写它。 As a reference you can use a gist I just created: https://gist.github.com/RockingRolli/79ceab04adb72c106cd6 I solved the issue a few weeks ago and it works. 作为参考,您可以使用我刚刚创建的要点: https : //gist.github.com/RockingRolli/79ceab04adb72c106cd6几周前我解决了这个问题,它可以工作。 The code basically is inheriting the TokenAuthentication and adds Mongoengine specific behaviour. 该代码基本上继承了TokenAuthentication并添加了Mongoengine特定的行为。 There are also Django user features provided by Mongoengine: http://docs.mongoengine.org/en/latest/django.html#custom-user-model - IIRC you also need them for MongoTokenAuthentication. Mongoengine还提供了Django用户功能: http ://docs.mongoengine.org/en/latest/django.html#custom-user-model-IIRC,MongoTokenAuthentication也需要它们。

All in all using Django (+Rest Framework) with Mongoengine can be tricky at some point and currently it looks like these issues will not be resolved soon. 总而言之,将Django(+ Rest Framework)与Mongoengine结合使用可能会有些棘手,目前看来这些问题不会很快解决。

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

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