So I have a dictionary of education levels :
education_levels = {
"Less than high school": 0,
"High school diploma or equivalent": 1,
"Postsecondary non-degree award": 2,
"Some college, no degree": 3,
"Associate's degree": 4,
"Bachelor's degree": 5,
"Master's degree": 6,
"Doctoral or professional degree": 7,
"#N/A": 100
}
I want to query my database so that it only shows education levels that are equal to or less than the inputted education level.
jobs = Occupation.query.filter(Occupation.area_name == area).filter(
education_levels[Occupation.typical_entry_level_education] <= education_levels[education])
However, this gives me a keyerror. I understand a keyerror is when u look something up that isn't a key. I'm 100% sure I've covered all possible keys from the database. I've even tried to search for something that's not in they key set using this:
def test():
jobs = Occupation.query.all()
job_list = []
for job in jobs:
if not(job.typical_entry_level_education in education_levels.keys()):
d = {
'ed': job.typical_entry_level_education
}
job_list.append(d)
return job_list
The output I get with my test() function is this
{
"jobs": []
}
I can't fathom how I'm getting a key error when everything is in the keyset. I even tried boolean checks in my query and it still gives me errors
here's the error I get (I'm sure its from the Occupation.typical_entry_level_education, because I've removed the latter and it still gives me the error)
education_levels[Occupation.typical_entry_level_education] <= education_levels[education])
KeyError: <sqlalchemy.orm.attributes.InstrumentedAttribute object at 0x3161e30>
The problem is that you're using the SQLAlchemy Column as a key and not it's value. Unfortunately, you can't really perform that query as you're trying. You could get the valid values first before making the query.
valid_levels = [k for k, v in education_levels.items() if v <= education_levels[education]]
jobs = Occupation.query.filter(Occupation.area_name == area).filter(Occupation.typical_entry_level_education.in_(valid_levels))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With