我对 python 有点陌生,我正在尝试使用在我的 Django 项目中使用序列化程序类序列化的外部 API,但出现以下错误
Traceback (most recent call last):
File "lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
TypeError: __init__() takes 1 positional argument but 2 were given
"GET /project/v1/mypath/ HTTP/1.1" 500 56241
看下面我的观点
import requests as requests
from rest_framework import viewsets
from rest_framework.response import Response
from .config import *
from .serializer import *
BASE_URL = BASE_URL
ACCOUNT_URL = "{}/v2/account".format(BASE_URL)
ORDERS_URL = "{}/v2/orders".format(BASE_URL)
HEADERS = {'APCA-API-KEY-ID': API_KEY, 'APCA-API-SECRET-KEY': SECRET_KEY}
# Create your views here.
class BrokerGetAccountView(viewsets.ModelViewSet):
serializer_class = BrokerAccountSerializer
queryset = BrokerAccount.objects.all()
def get_account(self):
r = requests.get(ACCOUNT_URL, headers=HEADERS)
serializer = BrokerAccountSerializer(r.json(), many=True)
return Response(serializer.data)
老实说,我不知道错误来自哪里 请告诉我如何解决这个问题以及错误在哪里 来自
还有哪个 __init__()
是错误所指的?
编辑 我编辑了问题中的代码实现了@bdbd 提出的建议 现在只返回空数组 ([])
请告诉我如何实际获取数据
答案 0 :(得分:0)
感谢大家的帮助 我最终是这样解决的,
import json
import requests as requests
from rest_framework import viewsets
from rest_framework.response import Response
from .serializer import *
from rest_framework.decorators import api_view, renderer_classes
from rest_framework.renderers import JSONRenderer, TemplateHTMLRenderer
# Create your views here.
@api_view(('GET',))
@renderer_classes((TemplateHTMLRenderer, JSONRenderer))
def BrokerGetAccountView(request):
import BrokerTrade.config
BASE_URL = BrokerTrade.config.BASE_URL
ACCOUNT_URL = "{}/v2/account".format(BASE_URL)
ORDERS_URL = "{}/v2/orders".format(BASE_URL)
HEADERS = {'APCA-API-KEY-ID': BrokerTrade.config.API_KEY, 'APCA-API-SECRET-KEY': BrokerTrade.config.SECRET_KEY}
request.META.get('HTTP_X_FORWARDED_FOR', '')
r = requests.get(ACCOUNT_URL, headers=HEADERS)
jsonData = r.json()
# print(jsonData)
serializer = BrokerAccountSerializer(data=jsonData)
if serializer.is_valid():
return Response(serializer.data)