Welcome to StackOverflow!
It appears your variable: mf_json_data
defined at: https://github.com/apalmesano2/assignment2_part2/blob/master/portfolio/models.py#L113 is failing to return a value, resulting in mf_request = None
.
I recommend you ensure your mf_json_data = requests.get(mf_url).json()
is pulling appropriately.
Try debugging with something like this:
mf_request = requests.get(mf_url)
print('MF Request:', mf_request, 'MF Request Type: ', type(mf_request))
if mf_request.status_code != 200:
print('Bad request')
return 'Unavailable'
mf_json_data = mf_request.json()
In any case, the error is occurring because your mf_request
is None
(causing the NoneType
exception).
EDIT: Based on your comment with the return value, I would say you should be handling cases where the return of .get('Global Quote')
is None
.
Try something like this:
def current_mutual_fund_price(self):
mf_symbol_f = str(self.symbol)
mf_main_api = 'https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol="
mf_api_key = "&apikey=REDACTED'
mf_url = mf_main_api + mf_symbol_f + mf_api_key
mf_json_data = requests.get(mf_url).json()
mf_open_price = float(mf_json_data.get('Global Quote', {}).get('05. price', 0))
mf_share_value = mf_open_price
return mf_share_value
Relevant update being:
mf_open_price = float(mf_json_data.get('Global Quote', {}).get('05. price', 0))
Obviously, you’ll want to update the your default in .get('05.price', 0)
with sane defaults or handle if the value is falsey.
Once you have updated this, you shouldn’t receive the error anymore, but you will need to handle cases where no values were found.
This is fairly speculative based on your question, though.
Additionally, I recommend you redact your secrets from Github.
You’ll want to rotate the secret defined at: https://github.com/apalmesano2/assignment2_part2/blob/master/portfolio/models.py#L111 and probably define it via the environment in settings.py
3
solved ‘NoneType’ object has no attribute ‘get’ when the object is not type None [closed]