如何在Django Rest Framework Authtoken

时间:2016-06-07 03:19:32

标签: django django-rest-framework django-rest-auth

我正在使用rest_framework.authtoken.models令牌。我可以看到3个字段,它们是key,created_at和user_id。

应用背景:

我使用chrome app作为app的客户端,我想使用令牌身份验证来连接我的django rest框架中的API。我想将user_id和company_id存储在 authtoken_token 表中。所以我只能将标记键存储在chrome app localstorage中,

enter image description here

  1. 我的问题是如何在该模型中添加像company_id这样的额外字段?我无法找到任何关于此的文档或文章。

  2. 我也在杰米的this article中回答了该模型的子类,但我不知道如何。

  3. 谢谢!

1 个答案:

答案 0 :(得分:2)

定义您自己的身份验证方法: settings.py

    'DEFAULT_AUTHENTICATION_CLASSES': (
    'my_project.my_app.authentication.myOwnTokenAuthentication',
     ),

authentication.py

from rest_framework.authentication import TokenAuthentication
from my_project.my_app.models.token import MyOwnToken

class MyOwnTokenAuthentication(TokenAuthentication):
    model = MyOwnToken

model.py

import binascii
import os

from django.db import models
from django.utils.translation import ugettext_lazy as _
from my_project.companies.models import Company


class MyOwnToken(models.Model):
    """
    The default authorization token model.
    """
    key = models.CharField(_("Key"), max_length=40, primary_key=True)

    company = models.OneToOneField(
        Company, related_name='auth_token',
        on_delete=models.CASCADE, verbose_name="Company"
    )
    created = models.DateTimeField(_("Created"), auto_now_add=True)

    class Meta:
        verbose_name = _("Token")
        verbose_name_plural = _("Tokens")

    def save(self, *args, **kwargs):
        if not self.key:
            self.key = self.generate_key()
        return super(MyOwnToken, self).save(*args, **kwargs)

    def generate_key(self):
        return binascii.hexlify(os.urandom(20)).decode()

    def __str__(self):
        return self.keyDefine you own authentication method:

settings.py

    'DEFAULT_AUTHENTICATION_CLASSES': (
    'my_project.my_app.authentication.myOwnTokenAuthentication',
     ),

authentication.py

from rest_framework.authentication import TokenAuthentication
from my_project.my_app.models.token import MyOwnToken

class MyOwnTokenAuthentication(TokenAuthentication):
    model = MyOwnToken

model.py

import binascii
import os

from django.db import models
from django.utils.translation import ugettext_lazy as _
from my_project.companies.models import Company


class MyOwnToken(models.Model):
    """
    The default authorization token model.
    """
    key = models.CharField(_("Key"), max_length=40, primary_key=True)

    company = models.OneToOneField(
        Company, related_name='auth_token',
        on_delete=models.CASCADE, verbose_name="Company"
    )
    created = models.DateTimeField(_("Created"), auto_now_add=True)

    class Meta:
        verbose_name = _("Token")
        verbose_name_plural = _("Tokens")

    def save(self, *args, **kwargs):
        if not self.key:
            self.key = self.generate_key()
        return super(MyOwnToken, self).save(*args, **kwargs)

    def generate_key(self):
        return binascii.hexlify(os.urandom(20)).decode()

    def __str__(self):
        return self.key