David
David

Reputation: 97

In Django why the error "TypeError at / Field 'fees' expected a number but got [[0, 0.26], [50000, 0.24], [100000, 0.22], ..."?

I am trying to figure out how to get in Django a sequence of strings, an integer, and an array of integers or numbers from a Kraken API. I went through other examples here and created the code below. The strings of my code return properly the values, however the arrays of integers return an error. How can i solve this error and gain control over every part of this API content?

views.py i marked the variables that "return OK" and the ones that do not return properly are "NOT OK"

from django.shortcuts import render
from meal_app.models import Kraken
import requests

def get_krakens(request):
    all_krakens = {}
    url ='https://api.kraken.com/0/public/AssetPairs' 
    response = requests.get(url)
    data = response.json()

    for i in data['result'].values():
        kraken_data = Kraken(
            altname = i['altname'], # string return OK
            wsname = i['wsname'], # string return OK
            aclass_base = i['aclass_base'], # string return OK
            base = i['base'], # string return OK
            aclass_quote = i['aclass_quote'], # string return OK
            quote = i['quote'], # string return OK
            lot = i['lot'], # string return OK
            pair_decimals = i['pair_decimals'], # integer return OK
            lot_decimals = i['lot_decimals'], # integer return OK
            lot_multiplier = i['lot_multiplier'], # integer return OK
            # leverage_buy = i['leverage_buy'], # Array of integers NOT OK
            # leverage_sell = i['leverage_sell'], # Array of integers NOT OK
            fees = i['fees'], # Array of Array of integers or numbers NOT OK
            # fees_maker = i['fees_maker'], # Array of Array of integers or numbers NOT OK
            fee_volume_currency = i['fee_volume_currency'], # string return OK
            margin_call = i['margin_call'], # integer return OK
            margin_stop = i['margin_stop'], # integer return OK
            # ordermin = i['ordermin'] # string NOT OK
            )
        kraken_data.save()
        all_krakens = Kraken.objects.all().order_by('-id')

    return render (request, 'meals/kraken.html', { "all_krakens": 
    all_krakens} )

As an example of a returned error, i uncomment the variable fees and returned the following error: enter image description here

How can i solve this error? Your help would be really valuable.

Note: when i comment out the variables that return error, my Django REST response is: enter image description here

Kraken doc here: kraken AssetPair API doc

Upvotes: 1

Views: 81

Answers (1)

David
David

Reputation: 97

I would like to share the approach and research that i found to solve the above problem:

Case 1 - API returns a list instead of a number. In the file views.py i changed the variable "fees" by including an index that would allow me to collect the info coming from a second layer in the nested "array of array of numbers" in the Kraken API:

fees = i["fees"][1][1]

API response schema:

enter image description here

Case 2 - wrong model format In models.py i changed the variable class from "IntegerField" to "FloatField" allowing me to return correctly a float number:

ordermin = models.FloatField(null=True, blank=True, default=None)

Source: https://docs.djangoproject.com/en/3.2/ref/models/fields/#django.db.models.FloatField

Upvotes: 0

Related Questions