如何解决“int”问题对象不可订阅”通过 Kraken API 在 Django 中
我想知道当存在字符串或整数数组的数组时,在 Django SQLite 中保存来自 Kraken API 的数字的正确方法是什么(https://docs.kraken.com/rest/#operation/getOHLCData)。
我的views.py
from rest_framework import generics
from .serializers import KrakenSerializer
from krakenohlc.models import Krak
import requests
class KrakenList(generics.RetrieveAPIView):
serializer_class = KrakenSerializer
queryset = Krak.objects.all()
def get_object(request):
url = 'https://api.kraken.com/0/public/OHLC?pair=XBTEUR'
response = requests.get(url)
data = response.json()
for i in data['result'].values():
kraken_data = Krak(
time_0=(i[0][0]),
)
kraken_data.save()
我的models.py
from django.db import models
class Krak(models.Model):
time_0 = models.IntegerField(blank=True, null=True)
def __str__(self):
return self.time_0
我在这里研究并尝试了许多类似的案例,但没有一个具有包含此错误消息的 API 响应示例。
I was wondering what the correct way is to save a number in Django SQLite coming from a Kraken API, when there is an Array of Array of strings or integers (https://docs.kraken.com/rest/#operation/getOHLCData).
my views.py
from rest_framework import generics
from .serializers import KrakenSerializer
from krakenohlc.models import Krak
import requests
class KrakenList(generics.RetrieveAPIView):
serializer_class = KrakenSerializer
queryset = Krak.objects.all()
def get_object(request):
url = 'https://api.kraken.com/0/public/OHLC?pair=XBTEUR'
response = requests.get(url)
data = response.json()
for i in data['result'].values():
kraken_data = Krak(
time_0=(i[0][0]),
)
kraken_data.save()
my models.py
from django.db import models
class Krak(models.Model):
time_0 = models.IntegerField(blank=True, null=True)
def __str__(self):
return self.time_0
This is the error that i get in the browser:
The SQLite response is actually saving the API number in the database:
I researched and tried thoroughly many similar cases here, but none had the example of an API response with this error message.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为问题在于“结果”数组中的最后一项 - “最后”。看起来这只是一个数字。我想你需要在算法中进行一些类型检查。
代码修改建议:
I think the issue is the with the last item in the "result" array of arrays - "last". It seems like it's just a number. I guess you need some type checking in the algorithm.
Suggestion for code modification: