简体   繁体   English

Django REST自定义身份验证

[英]Django REST custom authentication

Here's my issue guys. 这是我的问题。 I have a store user that can log to his site and view all kinds of data related to his store. 我有一个商店用户,可以登录到他的站点并查看与他的商店有关的各种数据。 In his store he has, let's say 10 android billing devices that send bills to him. 假设他在他的商店中有10台向他发送帐单的android计费设备。 Now I need to create a custom authentication in Django REST framework that will authenticate android devices with their id_number and type fields (not store user's username & password) and assign a token to them (each individually) and allow them to POST, GET, PUT etc.. Is that possible? 现在,我需要在Django REST框架中创建一个自定义身份验证,该身份验证将使用其id_numbertype字段(不存储用户的用户名和密码)对android设备进行身份验证,并为它们分配一个令牌(分别),并允许它们进行POST,GET,PUT等。这可能吗?

Store(models.Model):
    user = models.ForeignKey(User, default=1)

AndroidDevice(models.Model):
    id_number = models.CharField()
    type = models.CharField()
    store= models.ForeignKey(Store)

yes... it is possible 对的,这是可能的

Note: use a custom authentication and create your own Token Model that is related to you AndroidDevice model instead of the User Model or whatever you need to do (multiple tokens, tokens that expires, etc.). 注意:使用自定义身份验证并创建与AndroidDevice模型(而不是用户模型)或您需要执行的操作(多个令牌,过期的令牌等)相关的自己的令牌模型。 Read the DRF source code to get inspired :) 阅读DRF源代码以获取启发:)

Hope this helps 希望这可以帮助

What I ended up doing was adding another field to my model with OneToOne relationship to User model. 我最终要做的是通过与用户模型的OneToOne关系向我的模型添加另一个字段。 That way I was able to use DRF's built in authentication. 这样,我便可以使用DRF的内置身份验证。 Also I overrided the save method for that model so when the new instance is created it creates a new User object with his username/password set to values from id_number/type fields. 另外,我重写了该模型的save方法,因此在创建新实例时,它会创建一个新的User对象,其用户名/密码设置为id_number / type字段中的值。

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

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