TypeError in Django: “float () argument must be a string or a number, not ‘tuple.'”

huangapple go评论141阅读模式
英文:

TypeError in Django: "float () argument must be a string or a number, not 'tuple.'"

问题

以下是您要翻译的内容:

描述:

尝试访问与加密货币(在本例中是狗狗币)相关的特定页面时,我在我的Django应用程序中收到一个TypeError。错误通知中写着:“字段'购买价格'期望一个数字,但却得到了(0.07314770668745041,)”。回溯还说明问题出在float()函数上,并且一个元组被用作参数。

然而,传递的值并不是一个'元组'。它是numpyfloat64类型,因此我尝试在Python中将其转换为float类型后再传递相同的值。即使我提供表单一个静态值,问题仍然会发生。form.instance.bought_at = 45.66也导致了相同的问题。

是否有人可以帮助我理解为什么会发生这个错误以及如何解决它?

以下是相关的代码:

模型代码:

class Order(models.Model):
    
    coin = models.CharField(max_length=100)
    symbol = models.CharField(max_length=50)
    bought_price = models.FloatField()
    quantity = models.IntegerField(default=1)
    bought_at = models.DateTimeField(default=datetime.now)
    user = models.ForeignKey(User, on_delete=models.CASCADE)

视图代码:

ticker = yf.Ticker(symbols[pk])
price_arr = ticker.history(period='1d')['Close']
price = np.array(price_arr)[0]
if request.method == 'POST':
    form = BuyForm(request.POST)

    if form.is_valid():
        form.instance.coin = ticker.info['name'],
        form.instance.symbol = ticker.info['symbol'],
        form.instance.bought_price = price,
        form.instance.user = request.user
        form.save()
        return redirect('portfolio')
    else:
        form = BuyForm()

感谢您提供的任何帮助或见解。谢谢!

英文:

Description:

When attempting to access a specific page that relate to a cryptocurrency, in this case Dogecoin, I receive a TypeError in my Django application. "Field 'bought_price' expected a number but got (0.07314770668745041,)" reads the error notice. The traceback also states that the problem is with the float() function and that a tuple was used as the argument.

The passed value, however, is not a 'tuple'. It is a numpyfloat64 type, therefore I tried passing the same value after converting it to a float type in Python. Even if I provide the form a static value, the problem still occurs. form.instance.bought_at = 45.66 results in the same issue.

Could anyone help me understand why this error is occurring and how I can resolve it?

Here's the relevant code:

Model Code:

class Order(models.Model):

    coin = models.CharField(max_length=100)
    symbol = models.CharField(max_length=50)
    bought_price = models.FloatField()
    quantity = models.IntegerField(default=1)
    bought_at = models.DateTimeField(default=datetime.now)
    user = models.ForeignKey(User, on_delete=models.CASCADE)

View Code:

ticker = yf.Ticker(symbols[pk])
price_arr = ticker.history(period='1d')['Close']
price = np.array(price_arr)[0]
if request.method == 'POST':
    form = BuyForm(request.POST)

    if form.is_valid():
        form.instance.coin = ticker.info['name'],
        form.instance.symbol = ticker.info['symbol'],
        form.instance.bought_price = price,
        form.instance.user = request.user
        form.save()
        return redirect('portfolio')
    else:
        form = BuyForm()

I appreciate any assistance or insights you can provide. Thank you!

答案1

得分: 1

问题似乎是尾随逗号将 price 转换为元组。

        form.instance.bought_price = price,

应该是

        form.instance.bought_price = price

唯一的区别是末尾的逗号。其他赋值行前后可能也需要移除逗号。

查看此问题获取更多信息。

英文:

The problem appears to be the trailing comma turning price into a tuple.

        form.instance.bought_price = price,

should be

        form.instance.bought_price = price

The only difference is the comma at the end. The other assignment lines before and after probably need their commas removed as well.

Check out this question for more.

huangapple
  • 本文由 发表于 2023年5月30日 01:56:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76359431.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定