{"action":"closed","issue":{"url":"https://api.github.com/repos/MongoEngine/mongoengine/issues/884","labels_url":"https://api.github.com/repos/MongoEngine/mongoengine/issues/884/labels{/name}","comments_url":"https://api.github.com/repos/MongoEngine/mongoengine/issues/884/comments","events_url":"https://api.github.com/repos/MongoEngine/mongoengine/issues/884/events","html_url":"https://github.com/MongoEngine/mongoengine/issues/884","id":58066348,"number":884,"title":"Django support: MongoEngineBackend does not respect custom user model's USERNAME_FIELD","user":{"login":"mikelrob","id":3390342,"avatar_url":"https://avatars.githubusercontent.com/u/3390342?v=3","gravatar_id":"","url":"https://api.github.com/users/mikelrob","html_url":"https://github.com/mikelrob","followers_url":"https://api.github.com/users/mikelrob/followers","following_url":"https://api.github.com/users/mikelrob/following{/other_user}","gists_url":"https://api.github.com/users/mikelrob/gists{/gist_id}","starred_url":"https://api.github.com/users/mikelrob/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mikelrob/subscriptions","organizations_url":"https://api.github.com/users/mikelrob/orgs","repos_url":"https://api.github.com/users/mikelrob/repos","events_url":"https://api.github.com/users/mikelrob/events{/privacy}","received_events_url":"https://api.github.com/users/mikelrob/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/MongoEngine/mongoengine/labels/Django","name":"Django","color":"bfe5bf"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-02-18T13:18:07Z","updated_at":"2015-04-28T14:53:24Z","closed_at":"2015-04-28T14:53:24Z","body":"I've created a custom user model which does not have a username field, but rather uses the email field in lieu.\r\n\r\nI followed the article @ http://docs.mongoengine.org/django.html#custom-user-model, carrying out both modifications in the settings.py:\r\n```\r\nINSTALLED_APPS = (\r\n    ...\r\n    'django.contrib.auth',\r\n    'mongoengine.django.mongo_auth',\r\n    ...\r\n)\r\n\r\nAUTH_USER_MODEL = 'mongo_auth.MongoUser'\r\n```\r\nand importantly:\r\n```\r\nMONGOENGINE_USER_DOCUMENT = 'MongoLogOn.models.User'  # my custom User Model\r\n```\r\nMy custom `User` model specifies the following:\r\n```\r\nclass User(MongoUser):\r\n    USERNAME_FIELD = 'email'\r\n    REQUIRED_FIELDS = ['password']\r\n```\r\nIt is worth noting that the above inherited `MongoUserDoc` is a direct copy of `mongoengine.django.auth.User` with the `username` field removed as the 30 char limit is far too restrictive.\r\n\r\nThe trouble I'm having is that when I call `authenticate(<email>, <password>)` in my log in view, it returns `None`.\r\n\r\n\r\nI suspect the trouble is caused by the `MongoEngineBackend` as it blindly tries to retrieve the `User` document using the `username` field instead respecting the `USERNAME_FIELD` which is set in the custom `User` model as seen below:\r\n```\r\nclass MongoEngineBackend(object):\r\n    ...\r\n    def authenticate(self, username=None, password=None):\r\n        user = self.user_document.objects(username=username).first()\r\n```\r\n\r\nMy work around uses the following to circumvent the issue:\r\n```\r\nclass MongoEngineBackend(object):\r\n    ...\r\n    def authenticate(self, username=None, password=None):\r\n        filter_dict = {self.user_document.USERNAME_FIELD: username}\r\n        user = self.user_document.objects(**filter_dict).first()\r\n```\r\n\r\nSo far I have had success with this work around. "}}