简体   繁体   中英

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

PFB :- my resolved function

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'>

In my class Freelancer(graphene.ObjectType):

I have added 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.

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() . 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 ).

My guess would be to hunt down the float() call and remove it. If that doesn't work, I would then move onto Float.num field in your query.

Again, all conjecture here, but I hope it helped.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM