I am using SQLAlchemy to populate a database and often I need to check if a orm object exists in a database before processing. This may be an unconventional question, but I found myself encountering this pattern often:
my_object = session.query(SomeObject).filter(some_fiter).first()
if my_object: # Mostly in databases...
# Juchee it exists
# process
else:
# It does not exist. :-(
my_object = SomeObject()
# process
What I am dreaming of would be something like:
if my_object = session.query(someObject).blabla.first():
# if my_object is None this scope is left alone
# if my_object is not None I can work with my_object here...
I know, that this syntax is wrong, but I wanted to explain, what I mean by this example. Any equivalent way would make me happy.
Is there an elegant python approach for this pattern? This question aims not only at SQLAlchemy, but to each equivalent scenario.
closing my eyes hitting "Post your question" and waiting for the smart people and pythonistas by heart to hunt me down for asking something mayhaps inappropriate ;-)