简体   繁体   English

后端中的自定义身份验证-在运行时创建用户而不保存

[英]custom authentication in backend - Creating user at runtime without saving

While searching online on how to accomplish custom authentication in Django I came across this and this article. 虽然对如何在Django完成定制认证的在线搜索,我碰到这个这个文章。 Both of these articles specified the same instructions. 这两篇文章都指定了相同的说明。 Currently i have something like this. 目前我有这样的事情。 (Taken from first article) (摘自第一篇文章)

class Client(models.Model):
   email = models.EmailField(unique=True, max_length=100)
   password = models.CharField(max_length=128)

Then in another python file I have this 然后在另一个python文件中我有这个

from .models import Client

class ClientAuthBackend(object):

    def authenticate(self, username=None, password=None):
        try:
            user = Client.objects.get(email=username)
            return user

            if password == 'master':
                # Authentication success by returning the user
                return user
            else:
                # Authentication fails if None is returned
                return None
        except Client.DoesNotExist:
            return None

    def get_user(self, user_id):
        try:
            return Client.objects.get(pk=user_id)
        except Client.DoesNotExist:
            return None

I just started using Django and have kind of skipped the model section for db interaction since in my current project I am using RAW and custom SQL due to certain reasons. 我刚刚开始使用Django,由于某种原因,由于当前原因,我正在使用RAW和自定义SQL,因此跳过了数据库交互的模型部分。 My question is where does the 我的问题是

user = Client.objects.get(email=username)

get its user from . 从获得用户。 Do I have to make an entry into a database ? 我是否必须输入数据库? What I want to do is to create a user during runtime and not save to the database.I tried doing this 我想做的是在运行时创建一个用户而不保存到数据库中。

#The following creates and saves a user in the db
 u =User.objects.create_user('john', 'lennon@thebeatles.com', 'johnpassword',cust_address="my_custom_address",cust_msg="Users custom message")

The above returns a Client.DoesNotExist exception. 上面的代码返回Client.DoesNotExist异常。

My question is where does the 我的问题是

 user = Client.objects.get(email=username) 

get its user from 从中获取用户

Apparently, Client is a models.Model . 显然, Clientmodels.Model This means that represents a single record of the relevant table in your database, wherever that is depending on the relevant settings.py setting. 这意味着,它代表数据库中相关表的单个记录,无论该记录取决于相关的settings.py设置。

Therefore, the table representing that model can be created with the next Django migration and lots of other useful things Django allows for. 因此,可以在下一次Django迁移以及Django允许的其他许多有用功能中创建代表该模型的表。

So essentialy the above statement instructs the Django ORM to fetch all Client records from that particular table with that specific email. 因此,上述声明必不可少,它指示Django ORM使用该特定电子邮件从该特定表中获取所有Client记录。 If no such entries exist, none will be returned. 如果不存在此类条目,则不会返回任何条目。

I tried doing this 我尝试这样做

 u =User.objects.create_user('john', 'lennon@thebeatles.com', 'johnpassword',cust_address="my_custom_address",cust_msg="Users custom message") 

This is where you complicate things a bit. 这是您使事情复杂化的地方。 The create_user method is not part of Django ORM, but part of the Django default auth model manager django.contrib.auth.models.User . create_user方法不是Django ORM的一部分,而是Django默认身份验证模型管理器 django.contrib.auth.models.User You should either provide such a method yourself, or easier to just use the standard create method provided with the default Django model manager. 您应该自己提供这样的方法,或者更容易使用默认Django模型管理器随附的标准create方法。

Not saving the user model even on some cache does not make sense at all, as it implies that the user registers each time he or she wishes to login. 即使在某个缓存上也不保存用户模型根本没有意义,因为这意味着用户每次希望登录时都进行注册。

Having said all those, I would strongly recommend you to read the official Django documentation. 说完这些,我强烈建议您阅读官方的Django文档。 All the above are covered, the documentation is very comprehensive and not that long. 以上所有内容均已涵盖,文档非常全面,而且时间不长。 You can then read and understand tutorials on the wild which may or may not be correct or up to date. 然后,您可以阅读和理解狂野的教程,这些教程可能正确,也可能不正确或是最新的。

Take a good read specifically on the Customizing authentication topic , as it provides additional methods far easier for the beginner. 请仔细阅读有关“ 定制身份验证”主题的内容 ,因为它为初学者提供了更轻松的附加方法。

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

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