Wee
Wee

Reputation: 234

[sqlalchemy]SyntaxError: non-keyword arg after keyword arg

@classmethod
def get(cls, id=None, **kwarg):
    return con_sesison.query(cls).filter_by(or_(id=id, and_(**kwarg))).scalar()

theh I get the error:

SyntaxError: non-keyword arg after keyword arg

Upvotes: 0

Views: 841

Answers (2)

javex
javex

Reputation: 7544

filter_by is just a shorthand function to do equal comparison via keyword arguments. Instead almost always it is clearer to use filter which allows expressions:

exprs = [cls.k == v for k, v in kwarg.items]
con_session.query(cls).filter(or_(cls.id==id, and_(*exprs))).scalar()

The filter_by method is not very flexible but with the above you should be able to achieve the same (untested).

Upvotes: 0

alecxe
alecxe

Reputation: 473853

By using id=id you are passing an id argument with the value equals to id variable. Instead you need to pass filter conditions to or_ in format field == value.

Just replace id=id with id==id.

Upvotes: 1

Related Questions