简体   繁体   中英

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.

I've got the implementation to work but am stuck with Django Rest Framework's authentication system. Was just reading the API guide for their authentication chapter and was trying out their TokenAuthentication .

Tokens are created using

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

and it expects a Django User instance. Since I am using MongoEngine, my database entry in the settings.py file is set to Dummy.

So how do I create a user instance that can be used by Token class.

I tried creating users using MongoEngine's mongoengine.django.auth but the Token class isn't accepting this object.

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. The code basically is inheriting the TokenAuthentication and adds Mongoengine specific behaviour. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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