如何解决“int”问题对象不可订阅”通过 Kraken API 在 Django 中

发布于 2025-01-10 19:56:42 字数 1408 浏览 0 评论 0原文

我想知道当存在字符串或整数数组的数组时,在 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

这是我在浏览器中遇到的错误: 输入图片这里的描述

SQLite 响应实际上是将 API 编号保存在数据库中: 输入图片此处描述

我在这里研究并尝试了许多类似的案例,但没有一个具有包含此错误消息的 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:
enter image description here

The SQLite response is actually saving the API number in the database:
enter image description here

I researched and tried thoroughly many similar cases here, but none had the example of an API response with this error message.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深爱成瘾 2025-01-17 19:56:42

我认为问题在于“结果”数组中的最后一项 - “最后”。看起来这只是一个数字。我想你需要在算法中进行一些类型检查。
输入图片此处描述

代码修改建议:

for i in data['result'].values():
  # Skips "last" attribute
  if isinstance(i, int):
    continue
  kraken_data = Krak(
    time_0=(i[0][0]),
  )
  kraken_data.save()

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.
enter image description here

Suggestion for code modification:

for i in data['result'].values():
  # Skips "last" attribute
  if isinstance(i, int):
    continue
  kraken_data = Krak(
    time_0=(i[0][0]),
  )
  kraken_data.save()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文