Skip to content
Advertisement

TypeError: float() argument must be a string or a number, not ‘Profile’

Problem:

I’m trying to get the latest value from my model called Profile. But I’ve encountered a problem, when I try to save it to a variable as a float value I get this error TypeError: float() argument must be a string or a number, not 'Profile'. I need this so, I can make calculations with the datas.

Models.py file:

JavaScript

Views.py file (relevant parts):

JavaScript

I searched up this error code here, but I didn’t find any, where ppl wanted to convert from obj to float

Advertisement

Answer

The expression:

JavaScript

Does not return a float value, it returns the Profile with the highest weight. But not the weight itself.

You can however easily obtain both values by .aggregate(…) [Django-doc]:

JavaScript

Note that this not per se the person with the largest bmi. It will simply look for the largest weight and height of all Profiles. (Very) likely the data will originate from two different Profiles.

If you want to calculate the BMI of a Profile, you can use:

JavaScript

You can obtain the Profile with the largest primary key pk:

JavaScript

but that is not per se the latest added object.

Advertisement