Flask-SQLalchemy AttributeError: 'NoneType' object has no attribute ""

893 Views Asked by At

I am trying to query couple of tables , and using for loops I am adding filters to queries.

new_list = []
query = {
    "search_word": "Home",
    "exact": False,
    "tags": ["N"],
    "next_words": [
        {"pos": 1, "tags": ["verb"]},
        {"pos": 2, "tags": ["anim"]}
    ]
}

if query["search_word"]:
    if query["exact"]:
        db_query = Cases.query.filter(Cases.gramatical_case == query["search_word"])
    else:
        db_query = Cases.query.filter(Cases.gramatical_case.ilike(f'%' + query["search_word"] + '%'))

    for tag in query["tags"]:
        db_query = db_query.filter(Cases.grammer.contains(tag))

    for words in query["next_words"]:
        tagging = words['tags']  # list of values
        for rg in tagging:
            db_query = db_query.filter(Cases.pos.contains(rg))

    filter2=Case_Sentences.query.with_entities(Case_Sentences.st_id).filter_by(gc_id=db_query.first().gc_id).all()
    for r in filter2:
        new_list.append(r.st_id)

    filter3 = Sentences.query.filter(Sentences.sn_id.in_(new_list)).paginate(per_page=12,page=page_num,error_out=False)

Goal is, using for loops add filters to query and at the end call it and fetch it all.

And the error I am getting :

filter2=Case_Sentences.query.with_entities(Case_Sentences.st_id).filter_by(gc_id=db_query.first().gc_id).all()

AttributeError: 'NoneType' object has no attribute 'gc_id'

Why do I get this error message, even tho everything seems to be correct

1

There are 1 best solutions below

0
user20145543 On

The problem was in :

for words in query["next_words"]:
        tagging = words['tags']  # list of values
        for rg in tagging:
            db_query = db_query.filter(Cases.pos.contains(rg))

This query was returning Empty Object for a reason, Queries done here was wrong.