如何手动为Django序列化程序字段赋值?

如何手动为Django序列化程序字段赋值?,django,python-3.x,serialization,django-rest-framework,yelp,Django,Python 3.x,Serialization,Django Rest Framework,Yelp,我正在编写一个Django REST API服务器,它将从Yelp返回令牌/凭证信息。为了测试的目的,我把这个URL映射到一个端点,但我不知道如何用调用Yelp返回的信息填充序列化程序的字段 当我连接到端点时,我得到下面的响应-没有任何信息。。。我假设这是因为我需要在序列化程序的字段中设置值 { "client_id": "", "client_secret": "", "token_path": "", "grant_type": "", "token_

我正在编写一个Django REST API服务器,它将从Yelp返回令牌/凭证信息。为了测试的目的,我把这个URL映射到一个端点,但我不知道如何用调用Yelp返回的信息填充序列化程序的字段

当我连接到端点时,我得到下面的响应-没有任何信息。。。我假设这是因为我需要在序列化程序的字段中设置值

{
    "client_id": "",
    "client_secret": "",
    "token_path": "",
    "grant_type": "",
    "token_type": "",
    "expires_in": null,
    "access_token": ""
}
我的观点是

from rest_framework import generics
from .serializers import YelpAuthenticationSerializer
from .models import YelpAuthentication

# Create your views here.
class AuthView(generics.ListCreateAPIView):
    queryset = YelpAuthentication.objects.all()
    serializer_class = YelpAuthenticationSerializer

    def perform_create(self,serializer):
        pass
YelpAuthentication课程是

#yelp.com/developers/documentation/v3/authentication
class YelpAuthentication(models.Model):

    #fields necessary to authenticate
    url = models.CharField(max_length=200, blank=False, default='https://api.yelp.com/oauth2/token')
    api_host = models.CharField(max_length=100, blank=False, default='https://api.yelp.com')
    token_path = models.CharField(max_length=100, blank=False, default='/oauth2/token')
    grant_type = models.CharField(max_length=200, blank=False, default='client_credential')
    client_id = models.CharField(max_length=200, blank=True,default='N/A')
    client_secret = models.CharField(max_length=300, blank=True, default='N/A')

    #respone body elements after authentication
    access_token = models.CharField(max_length=200, blank=False, default='N/A')
    token_type = models.CharField(max_length=7, blank=False, default='bearer')
    expires_in = models.BigIntegerField()

    def authenticate(self):
       #some code that works
        ...
       return token
我想做的是…当我的序列化程序序列化数据时,字段值与YelpAuthentication具有相同的值,特别是,我希望access_标记是从YelpAuthentication函数返回的值

这就是我认为它应该如何工作

    from rest_framework import serializers
    from .models import YelpAuthentication

    class YelpAuthenticationSerializer(serializers.ModelSerializer):

        class Meta:

            model = YelpAuthentication

            #I know this part is wrong...but how do I do this...
            model.authenticate()
            serializer.client_id = model.client_id
            serializer.client_secret = model.client_secret
            serializer.access_token = model.authenticate()

            #..............more manual field settings......

            fields = ('id', 'client_id','client_secret','token_path','grant_type','token_type','expires_in','access_token')
            read_only_fields = ('url', 'api_host')
我的预期输出是

{
    "client_id": "N/A",
    "client_secret": "N/A",
    "token_path": "/oauth2/token",
    "grant_type": "client_credential",
    "token_type": "bearer",
    "expires_in": null,
    "access_token": "02UYO2UOUAOUF20H2NDJALNDAOFY020A80DSUFASJDFASJF03U0QUAFAHAOJSDFOUASDF0"
}