简体   繁体   English

float() 参数必须是字符串或数字,而不是 'Float'

[英]float() argument must be a string or a number, not 'Float'

I know this question has been asked before but none of the questions were helpful hence asking again..我知道以前有人问过这个问题,但没有一个问题有帮助,因此再次问..

I am using graphene and parsing some Elasticsearch data before passing it to Graphene我正在使用石墨烯并在将其传递给石墨烯之前解析一些 Elasticsearch 数据

PFB :- my resolved function PFB :- 我已解决的函数

def resolve_freelancers(self, info):
    session = get_session()
    [ids, scores] = self._get_freelancers()
    freelancers = session.query(FreelancerModel).filter(FreelancerModel.id.in_(ids)).all()

    for index in range(len(ids)):
        print("index", scores[index])
        freelancers[index].score = scores[index]

    if self.sort:
        reverse = self.sort.startswith("-")
        self.sort = self.sort.replace("-", "")
        if self.sort == "alphabetical":
            freelancers = sorted(freelancers, key=lambda f: f.name if f.name else "", reverse=reverse)
        if self.sort == "created":
            freelancers = sorted(freelancers, key=lambda f: f.created_on, reverse=reverse)
        if self.sort == "modified":
            freelancers = sorted(freelancers, key=lambda f: f.modified_at, reverse=reverse)
    freelancers = [Freelancer(f) for f in freelancers[self.start:self.end]]
    session.close()
    return freelancers

now if I do现在如果我这样做

print(freelancers[index].score)

it gives me 10.989184 and the type of this is <class 'float'>它给了我10.989184 ,它的类型是<class 'float'>

In my class Freelancer(graphene.ObjectType):在我的class Freelancer(graphene.ObjectType):

I have added score = graphene.Float()我添加了score = graphene.Float()

Now when I try to add score to my query it gives the error .. otherwise there is no issue .. all I am interested is in getting that score value in the json response .. I do not understand what is causing this error and I am fairly new to Python so any advise will be appreciated.现在,当我尝试将score添加到我的查询时,它会给出错误.. 否则没有问题.. 我感兴趣的是在 json 响应中获取该分数值.. 我不明白是什么导致了这个错误,我我对 Python 相当陌生,因此任何建议都将不胜感激。

Please feel free to ask for additional code or information as I have tried to paste whatever I thought was relevant请随时询问其他代码或信息,因为我已尝试粘贴我认为相关的任何内容

So I can't comment or I would, and I very well may be wrong, but here goes.所以我不能评论或者我会评论,我很可能是错的,但这里是。

My guess is that somewhere you are calling float(score) , but the graphene.Float() type cannot be directly converted to a Python float via float() .我的猜测是,您在某个地方调用了float(score) ,但graphene.Float()类型无法通过float()直接转换为 Python 浮点数。 This is probably due to the graphene.Float type having so much data it can hold in its data structure due to inheriting from graphene.Scalar ( graphene GH/Scalars ).这可能是由于graphene.Float类型由于继承自graphene.Scalar ( graphene GH/Scalars ),它可以在其数据结构中保存如此多的数据。

My guess would be to hunt down the float() call and remove it.我的猜测是寻找float()调用并将其删除。 If that doesn't work, I would then move onto Float.num field in your query.如果这不起作用,那么我将转到查询中的Float.num字段。

Again, all conjecture here, but I hope it helped.再次,所有猜测都在这里,但我希望它有所帮助。

实际上我不能将字段直接传递给石墨烯对象,我们需要在具有石墨烯对象的类的init方法中传递它,然后我们需要在解析器方法中返回(在我的例子中是 resolve_score )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 float参数必须是字符串或数字 - float argument must be a string or a number 类型错误:float() 参数必须是字符串或数字,而不是“Day” - TypeError: float() argument must be a string or a number, not 'Day' float()参数必须是字符串或数字,而不是&#39;Timestamp&#39; - float() argument must be a string or a number, not 'Timestamp' float()参数必须是字符串或数字,而不是&#39;zip&#39; - float() argument must be a string or a number, not 'zip' 类型错误:float() 参数必须是字符串或数字,而不是“配置文件” - TypeError: float() argument must be a string or a number, not 'Profile' 类型错误:float() 参数必须是字符串或数字,而不是“Period” - TypeError: float() argument must be a string or a number, not 'Period' TypeError:float()参数必须是字符串或数字,而不是&#39;方法&#39; - TypeError: float() argument must be a string or a number, not 'method' TypeError:float() 参数必须是字符串或数字,而不是“SimpleImputer” - TypeError: float() argument must be a string or a number, not 'SimpleImputer' TypeError: float() 参数必须是字符串或数字,而不是“Tensor” - TypeError: float() argument must be a string or a number, not 'Tensor' TypeError:float() 参数必须是字符串或数字,而不是“类型” - TypeError: float() argument must be a string or a number, not 'type'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM