2016-06-07 3 views
1

Ich benutze rest_framework.authtoken.models Token. Ich kann 3 Felder sehen, die Schlüssel, created_at und user_id sind.So fügen Sie ein Feld im Token-Modell in Django Rest Framework hinzu Auttoken

Hintergrund der App:

I Chrom App als Client für die App verwenden, mag ich Token-Authentifizierung verwenden, um mit meiner APIs in django Rest Rahmen zu verbinden. und ich möchte user_id und company_id in authtoken_token Tabelle speichern. so kann ich nur speichere den Token-Schlüssel in Chrom App local,

enter image description here

  1. Meine Frage ist, wie kann ich ein zusätzliches Feld wie company_id zu diesem Modell hinzufügen? Ich konnte keine Dokumente oder Artikel darüber finden.

  2. Ich habe auch Jamies Antwort in this article zu Unterklasse das Modell, aber ich weiß nicht wie.

Vielen Dank!

+0

Check [Multi-Table Vererbung] (https://docs.djangoproject.com/en/1.9/topics/db/models/# Multi-Table-Vererbung) –

Antwort

0

definieren Sie Authentifizierungsmethode besitzen: 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 
Verwandte Themen