I have the following code where I'm querying my peewee database. I'm getting an error in the for loop on the line that has term.sets_term_id. This is the error TermsDoesNotExist: Instance matching query does not exist: If I do vars(term) then sets_terms_id is an option. Why am I getting that error and how do I fix it?
def get_api_response(id):
response = {}
print("id is " + str(id))
try:
sets = models.Sets.select().where(models.Sets.user_id == id)
except models.DoesNotExist:
return json.loads({'error' : 'that set does not exist'})
else:
print("it was successful")
for term in sets:
print(term.sets_term_id)
Here are my Sets and Terms models
class Terms(UserMixin, BaseModel):
term_id = CharField()
sets_id = CharField()
rank = IntegerField()
term = TextField()
definition = TextField()
@classmethod
def include_term(cls, set_id, term_id, definition, rank, term, **kwards):
try:
cls.select().where(cls.term_id == term_id).get()
except cls.DoesNotExist:
print("putting term into db")
concept = cls(
sets_id = set_id,
term_id = term_id,
definition = definition,
rank = rank,
term= term)
concept.save()
print(concept.term)
print("term saved to db")
return concept
else:
raise Exception("Term with that id already exists")
class Sets(UserMixin, BaseModel):
user_id = CharField()
name_set = CharField()
#created_date = DateField()
#modified_date = DateField()
#published_date = DateField()
sets_term_id = ForeignKeyField(Terms, to_field="sets_id")
@classmethod
def include_set(cls, user_id, name_set, sets_term_id, **kwards):
try:
cls.select().where(
(cls.name_set == name_set) | (cls.sets_term_id == sets_term_id)
).get()
except cls.DoesNotExist:
print("putting set into db")
quizlet_set = cls(
user_id = user_id,
name_set = name_set,
sets_term_id = sets_term_id)
print(quizlet_set.name_set)
quizlet_set.save()
print("set saved in db")
return quizlet_set
else:
raise Exception("Set with that name already exists")
The problem is in your except block.
except models.DoesNotExist:
return json.loads({'error' : 'that set does not exist'})
Should be:
except models.Sets.DoesNotExist:
return json.loads({'error' : 'that set does not exist'})
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